<body>
<input type="text" />
<input type="button" value="检测" />
<script>// alert( detectNum('123456') );
var aInp = document.getElementsByTagName('input');aInp[1].onclick = function () {
var val = aInp[0].value;
if ( detectNum(val) ) {
alert( '恭喜,'+ val +'全是数字' );
} else {
alert('输入有误');
}
};
function detectNum ( str ) {
var n = 0;
for ( var i=0; i<str.length; i++ ) {
n = str.charCodeAt(i);
if ( n<48 || n>57 )return false;
}
return true;
}
</script>
</body>
c判断字符串是不是数字_判断是不是都是数字(charCodeAt)
最新推荐文章于 2023-11-24 10:39:20 发布