<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.111cn.net/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312" />
<title>js 只能输入数字代码</title>
<script>
function check()
{
if(!isnan(myform.str.value))
{
alert("数字");
}
}
</script></head>
<body>
用正则表达式限制只能输入数字:
<form name="myform">
<input name="str" onkeyup="value=value.replace(/[^d]/g,'');" onbeforepaste="clipboarddata.setdata('text',clipboarddata.getdata('text').replace(/[^d]/g,''));" />
<input name="" type="button" onclick="check();" value="验证是否为全部数字"/>
</form>
</body>
</html>
时间: 2024-10-25 08:20:00