Click to See Complete Forum and Search --> : Decimal number valiation


shanuragu
06-16-2003, 10:13 AM
HI

How to validate a decimal(12.00) value for a text field.

ShaRa

vickers_bits
06-16-2003, 10:22 AM
how do u want it validated?
if all you want is a decimal point(.) and it must be a number

var myNumber=document.myForm.MyText.value;
if(!isNaN(myNumber) && myNumber.indexOf(".")>-1){
//process number
}else{
alert("Enter A Decimal Number");
return false;
}

Jona
06-16-2003, 10:28 AM
You can also use RegExps. The below code has not been tested but you might want to try it, it's much simpler:


var re = /\d\d\.\d\d/
var textBox = document.myForm.myTextBoxToValidate;
if(re.test(textBox.value)){
//process number
} else {
alert("Please use a decimal number in the format: xx.xx");
return false;}


Jona

pyro
06-16-2003, 11:09 AM
var re = /\d\d\.\d\d/
should probably be:
var re = /^\d{2}\.\d{2}$/

Jona
06-16-2003, 11:15 AM
I just tested my code and it works okay... But I guess I didn't do it right completely.. But wait, I just thought about it and shouldn't {2} be {2,} with the comma?

pyro
06-16-2003, 11:21 AM
Actually, your's does not work. It will allow this: 12.001. Also, {2,} would mean 2 or more, so you may want to use it with the first one, but probably not the second.

var re = /^\d{2,}\.\d{2}$/

to allow 123.00, 125212.12, but not 12.321, 1242.1 or 1.93

Jona
06-16-2003, 11:29 AM
Oh yes I see. Thanks. ;)

Jona