Home >Web Front-end >JS Tutorial >How does javascript determine whether the data entered in the control is a numeric type?
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.
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>
[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!