【发布时间】:2013-08-26 04:13:02
【问题描述】:
在javascript中验证一个文本字段值,该值可能是正整数或带一个小数点和一个小数点后的数字:
123
1
12345
233.2
1212.2
1.1
是有效数字和
1.222
1.33
-89789
-3
是无效数字。
我已经应用了这个但没有得到想要的结果
function CheckOneDecimal(txtbox) {
if (txtbox.value.length > 0)
{
if (isNaN(txtbox.value)) {
txtbox.value = "";
alert("Please enter number with one decimal places");
txtbox.focus();
return;
}
else if (parseFloat(txtbox.value) > 0)
{
txtbox.value = "";
alert("Please enter number with one decimal places. eg. 8.1");
txtbox.focus();
return;
}
var oRegExp = /^\s*\d+\.\d{1}\s*$/;
if (oRegExp.test(txtbox.value))
{ }
else {
txtbox.value = "";
alert("Please enter number with one decimal places");
txtbox.focus();
}
}
}
【问题讨论】:
-
“但没有得到想要的结果” - 你得到了什么做什么?
-
我只是在验证后得到空的 txtbox。如果 txt 的值为 3.444 或 32323,则仅在其值为 4.5 时才有效。但是通过以下解决方案可以正常工作,谢谢您的回复
标签: javascript regex validation