View Full Version : Number Validation in JScript in Windows IE 6

09-27-2005, 10:23 AM
Hi there - I am very new to JScript - and need some help with this form validation.

I have the following JScript doing some validation for me and I would like to extend the validation.

At the moment it validates for "is a number" - but I need it to validate the following:
Is a number, only 2 decimal places, maximum of 10 digits, NO minus values, only full stop allowed as decimal seperator.

Please can somebody suggest the correct syntax for this function !

Current Code:-
<input type="text" name="M0" size="12" class="input" value="" onChange="if (isNaN (this.value)) {alert('KPI Values should contain ONLY NUMBERS...Please check your last entry');this.focus()}>

I also used this code:-
function checkForNumeric(field)
// only allow numbers to be entered
var checkOK = "0123456789";
var checkStr = field.value;
var allValid = true;
var allNum = "";
for (i = 0; i < checkStr.length; i++)
ch = checkStr.charAt(i);
for (j = 0; j < checkOK.length; j++)
if (ch == checkOK.charAt(j))
if (j == checkOK.length)
allValid = false;
if (ch != ",")
allNum += ch;
if (!allValid)
alert("KPI Values should contain ONLY NUMBERS...Please check your last entry");
return (false);

Many thanks in advance


09-27-2005, 01:28 PM
Regular Expressions (http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Guide#Regular_Expressions) is the right tool for this job

09-28-2005, 12:05 PM
Thanks - I did think that was the way to go - now I just need the regular expression involved

Kind Regards