日期:2014-05-16 浏览次数:20368 次
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB2312" />
<title>Untitled Document</title>
</head>
<body>
<input type="text" id="myInput" value="" />
<input type="button" value="确定" id="myButton" />
</body>
</html>
<script language="JavaScript" type="text/javascript">
function $(obj){
return document.getElementById(obj);
}
function checkIsInteger(str)
{
//如果为空,则通过校验
if(str == "")
return true;
if(/^(\-?)(\d+)$/.test(str))
return true;
else
return false;
}
String.prototype.trim = function()
{
return this.replace(/(^[\s]*)|([\s]*$)/g, "");
}
$("myButton").onclick=function(){
if(checkIsInteger($("myInput").value.trim())){
alert("成功");
}else{
alert("只能是数字");
}
}
</script>
?JS驗證數字
?