在w3c上做一个例子的时发现的【w3c直通车】
发现如果输入数字加一个英文点号居然也是可以通过的例如“10.”,不解,求大神介绍。。
附上代码:
<p>Please input a number:</p>
<input id="numb" type="text">
<button type="button" onclick="myFunction()">Click Me!</button>
<p id="demo"></p>
<script>
function myFunction() {
//Get the value of input field with id="numb"
var val = document.getElementById("numb").value;
//Get the element with id="demo"
var elem = document.getElementById("demo");
//If value is space or not a number
if (val == "" || isNaN(val)) {
elem.innerHTML = "Not a Number";
} else {
elem.innerHTML = "Input OK";
}
}
</script>
isNaN这个函数只会对NaN这一个值返回true,其他一律是false==是不该出现的特异点,黑洞,撒旦之子onclick也是不该出现的parseFloat和isNaN组合就可以了10.+3123e-207550xabc之类的东西,那么应该用正则来做这个检测比较靠谱