AikenDrum
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))
break;
if (j == checkOK.length)
{
allValid = false;
break;
}
if (ch != ",")
allNum += ch;
}
if (!allValid)
{
alert("KPI Values should contain ONLY NUMBERS...Please check your last entry");
return (false);
}
}
Many thanks in advance
AikenD
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))
break;
if (j == checkOK.length)
{
allValid = false;
break;
}
if (ch != ",")
allNum += ch;
}
if (!allValid)
{
alert("KPI Values should contain ONLY NUMBERS...Please check your last entry");
return (false);
}
}
Many thanks in advance
AikenD