<script>
function check(str){
var exp=/^\d+(\.\d+)?$/;
var x=exp.test(str);
if(!x)
alert("请输入数字!");}</script>
<body><input type="text" onchange="ss(this.value)"></body>
function check(str){
var exp=/^\d+(\.\d+)?$/;
var x=exp.test(str);
if(!x)
alert("请输入数字!");}</script>
<body><input type="text" onchange="ss(this.value)"></body>
Description: Matches any floating point numer/numeric string, including optional sign character (+ or -). Also matches empty strings.
Matches: [123], [+3.14159], [-3.14159] [ More Details]
Non-Matches: [abc], [3.4.5], [$99.95]
var objRegExp = /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/;
return objRegExp.test(strValue);
}
if(field.value==""){
alert("请输入!");
field.focus();
}else if(isNaN(field.value)){
alert("请输入数字!");
field.value="";
field.focus();
}
}
說明: ^表示開始匹配位置, 如果在中括號內,表示除…以外的字符
$表示結束匹配位置
{n,m}表示前面字符串的匹配次數,n為最小次字,m為最大次數
[+|-]{0,1} 表示匹配 “+” 或 “–“ 一次, 或者無符號
[ ] 如果後面不注明匹配次數,則默認為一次.
<script>
function isNumber(str)
{
var re=/^(-|\+)?\d+(\.\d+)?$/;
if(!re.test(str)){alert("不是数字")}
}
</script>
<input type=text onblur="isNumber(this.value)">