Home > Web Front-end > JS Tutorial > How does javascript determine whether the data entered in the control is a numeric type?

How does javascript determine whether the data entered in the control is a numeric type?

青灯夜游
Release: 2022-01-12 11:36:34
Original
3135 people have browsed it

Judgment method: 1. Use the "input control object.value" statement to obtain the data entered by the user; 2. Use "var re = /^[0-9] .?[0-9]*/; " statement defines a regular expression object; 3. Use the "re.test (input data)" statement to determine whether the input data is a numeric type through a regular expression.

How does javascript determine whether the data entered in the control is a numeric type?

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

javascript determines that the data entered in the control is of numerical type

  • First you need to use the value attribute to get the value of the input control ;

  • Then use regular expressions and the test() function to determine whether the data entered in the control is of numerical type.

Implementation code:

<input id="num" placeholder="请输入数值" /><br><br>
<button id="btn">判断类型</button>
<script>
	function my(id) {
		return document.getElementById(id);
	}
	my("btn").onclick = function() {
		var val = my("num").value;
		var re = /^[0-9]+.?[0-9]*/;//判断字符串是否为数字//判断正整数/[1−9]+[0−9]∗]∗/
		if (re.test(val)) {
			console.log("输入的是数值类型");
		} else {
			console.log("输入的不是数值类型,请重新输入");
		}
	}
</script>
Copy after login

How does javascript determine whether the data entered in the control is a numeric type?

[Related recommendations: javascript learning tutorial]

The above is the detailed content of How does javascript determine whether the data entered in the control is a numeric type?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template