Validating a text field value which may be positive whole number or number with one decimal point and one decimal after the point in javascript:
123
1
12345
233.2
1212.2
1.1
are valid numbers and
1.222
1.33
-89789
-3
are invalid numbers.
Ihave applied this but not get desired result
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();
}
}
}