封装检测数字的方法

来源:互联网 发布:淘宝交易流程 编辑:程序博客网 时间:2024/04/30 05:39
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>无标题文档</title></head><body><input type="text" /><input type="button" value="检测" /><script>var alnp = document.getElementsByTagName('input');alnp[1].onclick = function(){var val = alnp[0].value;if(detectNum(val)){alert('恭喜全是数字');}else{alert("不是数字");}} var n;function detectNum(str){for(var i=0; i<str.length;i++){n = str.charCodeAt(i);if(n<48 || n>57){return false;}}return true;}</script></body></html>

0 0