第十一章 判断已输入的文字是不是数字

wKiom1gHEN2Q445TAACRcIZ8s3U214.png-wh_50


<script>ide


// alert( detectNum('123456') );spa


var aInp = document.getElementsByTagName('input');blog


aInp[1].onclick = function () {ip

var val = aInp[0].value;get

if ( detectNum(val) ) {input

alert( '恭喜,'+ val +'全是数字' );it

} else {io

alert('输入有误');function

}class

};


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>

相关文章
相关标签/搜索