PDA

View Full Version : forms validation



krraleigh
01-11-2009, 08:43 AM
I am having a problem understanding the return value on my form validation
I thought that with the following script:


<form action="index.php" name="myform" onSubmit="return validate()">

That if my user entered invalid data and my function returned false that the forms page in question would halt the process and reload the page in question.
The code in question:


function validate (divId, theValue, typeInfo){

//instantiate object
var fvObj = new ValidatorObj();

if (typeInfo == "fName" || typeInfo == "lName"){
if(!validName(divId, theValue, fvObj))
return false;
} else if (typeInfo == "address"){
if(!validAddress(divId, theValue, fvObj))
return false;
} else if (typeInfo == "email"){
if(!validEmail(divId, theValue, fvObj))
return false;
} else if (typeInfo == "phone"){
if(!validPhone(divId, theValue, fvObj))
return false;
} else {
if(!validZip(divId, theValue, fvObj))
return false;
}

}

If my user types in the wrong zipcode I get an error message using onblur() when the user leaves the field. But, they can still submit the form to index.php, which I definately don't want to happen.

Can someone give me a heads up on what I am missing? I ran an alert against validZip() and works the way it should.

Kevin

diltony
01-11-2009, 11:19 AM
The full code is not here. And the ValidatorObj() - i cant see the source code of that class. Then u also need to tell us the kind of data that was entered into it that it failed to validate.

jscheuer1
01-12-2009, 03:48 AM
Yes, and if the function returns false, the page shouldn't reload. What should happen is that the form simply doesn't submit. This may or may not be accompanied by an error message explaining the problem to the user.

Validating forms in this manner is generally a bad idea. Validation should be done server side to prevent invalid records being created by users with javascript disabled.

But if server side validation is already in place, javascript validation can serve as an aid, a first line of defense if you will - taking a load off of the server.

If you want more help:

Please post a link to the page on your site that contains the problematic code so we can check it out.