How can I check that an input textbox has a float value with no more than 3 places after the point in javascript?
maybe add a case for .123
cobbal
2009-07-03 09:45:58
+1
A:
Multiply it by 1000, subtract the rounded value, and check if it's bigger than 0?
drvdijk
2009-07-03 09:41:44
A:
/^\d*\.\d{0,3}$/.test(field.value); // returns true if valid, otherwise returns false
NickFitz
2009-07-03 11:00:28
A:
thank u guys But i made the solution in a much simpler way : 1st wrote this function ----> //Checking A value is 3 decimal palce or not
function chkplace3(ctrl,msg) {
var dot="."
var val = ctrl.value;
var len=val.length
var ldot=val.indexOf(dot)
var diff=(len-ldot)
if(diff>4)
{
alert(msg + ' can not be more than 3 numeric places');
ctrl.focus();
return false;
}
return true;
}
and then calling with the reqd parameter from the form --->
if(!chkplace3(document.from.txtfield,'msg')) { return false; }
santanu
2009-07-03 11:04:35
That would allow me to enter "abc.def". The regular expression solutions posted by myself and others also check that only digits (0 - 9) are entered.
NickFitz
2009-07-03 11:47:27