I am using a function (below) for validation. However, if the first condition is true then all the conditions after do not execute, e.g. the form doesnt alert any error messages…any ideas? (code is below)
// chkNumeric is an external function, see at the end.
function validate_form ( )
{
valid = true;
if( ( document.xform.name.value == "" ) || ( document.xform.terms.value == "" )|| ( document.xform.terms.value=="" )
|| (document.xform.buiness_name.value=="")|| ( document.xform.location.value =="" )|| ( document.xform.min_qty.value=="" )
|| ( document.xform.max_qty.value == "") || ( document.xform.description.value == "" ) )
{
$.prompt ('Please complete all required fields (* is required)');
valid = false;
}
if(chkNumeric(document.xform.min_qty.value)==false)
{
$.prompt('Minimum quantity must be a number!');
valid=false;
}
if(chkNumeric(document.xform.max_qty.value)==false)
{
$.prompt('Maximum quantity must be a number!');
valid=false;
}
return valid;
}
function chkNumeric(strString)
// strString is the string passed to be checked
// check for valid numeric strings
{
// strvalidchars defines the set of characters which are valid in a numeric field
var strValidChars = “0123456789.-”;
// strings
var strChar;
// boolresult is the variable which returns true is string is in correct format or false if it is not a valid numeric string
var boolResult = true;
if (strString.length == 0) return false;
// test strString consists of valid characters listed above
for (i = 0; i < strString.length && boolResult == true; i++)
{
strChar = strString.charAt(i);
if (strValidChars.indexOf(strChar) == -1)
{
boolResult = false;
}
}
return boolResult;
}
yes $.prompt works fine, as it is executing if I submit the form and the first “if” is false, the other if’s also get executed at the same time, but if the first “if” is true, the others dont get executed independantly
Since variable declarations are automatically hoisted to the top of the existing scope, it’s preferred to declare all variables at the start of the function that they’re in, so as to reduce any confusion.
var i;
for (i = 0; i < strString.length && boolResult == true; i++) {
Also, one of the useful benefits of placing variable declarations at the start is that it’s easier to see if lots of unrelated variables are being used. If that’s the case then it’s a good early warning that you should split things up in to a couple of separate functions.