<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>
转载于:https://blog.51cto.com/11180930/1863402