...

View Full Version : Validate number



robert orlini
05-27-2004, 07:14 PM
I have a function to make sure a user enters a number. How can I include a number and a comma and period if a user wants to enter a price such as: 9.90?

The line:
if(charVal < "0" || charVal > "9" && charVal != "." && charVal != ",")[code] still prevents a comma and period.

[code]function isNumber2(inputStr, field)
{
for(var i=0; i<inputStr.length; i++)
{
var charVal = inputStr.substring(i, i+1);
if(charVal < "0" || charVal > "9" && charVal != "." && charVal != ",")
{
alert("Please make sure your " + field + " entry are numbers ., only");
return false;
}
}
return true;
}

Robert O.

Philip M
05-27-2004, 08:50 PM
var charVal = inputStr;
if (/[^0-9\.\,]/gi.test(charVal.value) || (charVal.value=="" || charVal.value =="." || charVal.value == ",")) { /// only digits, space, stop or comma allowed, but stop and comma without a digit are invalid

alert ("Test returned true - match was found"); // i.e. invalid characters are present
return false;
}
return true; // i.e. field contains only valid characters.

robert orlini
05-27-2004, 09:18 PM
Thanks Phillip. It works only the opposite of what I was looking for and that's my fault for not explaining things correclty.

How can the script you sent be revised so that if a customer enters anything BUT 0-9 a comma and a period a alert appears?

I'm kinda new to JavaScript.

Robert O.

shlagish
05-27-2004, 11:03 PM
the exact opposite?


var charVal = inputStr;
if (/[^0-9\.\,]/gi.test(charVal.value) || (charVal.value=="" || charVal.value =="." || charVal.value == ",")) { /// only digits, space, stop or comma allowed, but stop and comma without a digit are invalid

alert ("Test returned true - match was found"); // i.e. invalid characters are present
return true;
}
return false; // i.e. field contains only valid characters.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum