tags:

views:

50

answers:

5

How can I check that an input textbox has a float value with no more than 3 places after the point in javascript?

A: 

check this

Adinochestva
+2  A: 
if (textbox.value.match(/\d+(\.\d{1,3})?/) {
}
Philippe Leybaert
maybe add a case for .123
cobbal
+1  A: 

Multiply it by 1000, subtract the rounded value, and check if it's bigger than 0?

drvdijk
elegant (:
peirix
A: 
/^\d*\.\d{0,3}$/.test(field.value);  // returns true if valid, otherwise returns false
NickFitz
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
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