<!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>
封装检测数字的方法
最新推荐文章于 2023-04-07 09:09:21 发布