0

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

4 Answers 4

3
if (textbox.value.match(/\d+(\.\d{1,3})?/) {
}
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0
/^\d*\.\d{0,3}$/.test(field.value);  // returns true if valid, otherwise returns false

Comments

-2

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; }

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.