当前位置:编程学习 > JS >>

js限制只能输入数字

这是一款js限制只能输入数字实例函数,经过测试,完全可以,函数的实例原理是利用正则表达式来判断用户输和字符串是不是全数字。

<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.zzzyk.com/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312" />
<title>js限制只能输入数字</title>
<script language="网页特效">
 function ispositive(strnumber)
 {
  re=/^[0-9]+.{0,1}[0-9]{0,2}$/;
  var str = strnumber.replace(/(^s*)|(s*$)/g, "");
  if(str == "")
   return true;
  else
   return (re.test(str))?true:false;
 
 }
 
 function ck()
 {
  var value = document.getelementbyid('numeric').value;
  if( ispositive( value ) &&  value !="" )
  {
   alert('全部是数字');
  }
  else
  {
   alert('必须全是数字');
   form1.numeric.select();
  }
 }
</script>
</head>

<body>
<form id="form1" name="form1" method="post" action="">
  <label for="textfield"></label>
  <input type="text" name="numeric" id="numeric" />
  <input type="button" name="button" id="button" value="按钮"   onclick="ck();"/>
</form>
</body>
</html>

 

补充:网页制作,js教程 
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,