js检测输入的是否全部为数字

来源:互联网 发布:矩阵奇异值分解 matlab 编辑:程序博客网 时间:2024/05/17 00:55
1、charCodeAt()  数字0-9: 48-57英语小写字母 a-z:97-122英语大写字母 A-Z:65-902、大体思路:判断charCodeAt()解析后的数组是否在48-57之间,如果不是则返回false<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title></title>    <script>        window.onload = function(){            var oText = document.getElementById('text1');            var oBtn = document.getElementById('btn1');                        oBtn.onclick = function(){                var s = oText.value;                if(a(s)){                    alert(''+s+'全是数字')                }else{                    alert(''+s+'不全是数字')                }            };                         function a(){                var n = 0;                for(i=0;i<oText.value.length;i++){                    n = oText.value.charCodeAt(i);                    if(n<48 ||n>57){                        return false;                      }                    else{                        return true;                    }                }            }        }    </script></head><body>    <input id="text1" type="text" value="" placeholder="请输入...">    <input id="btn1" type="button" value="检测"></body></html>
0 0
原创粉丝点击