Results 1 to 3 of 3

Thread: forms validation

  1. #1
    Join Date
    Jun 2007
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default forms validation

    I am having a problem understanding the return value on my form validation
    I thought that with the following script:

    Code:
    <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:

    Code:
    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

  2. #2
    Join Date
    Dec 2008
    Location
    Nigeria
    Posts
    95
    Thanks
    3
    Thanked 8 Times in 8 Posts

    Default

    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.

  3. #3
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    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.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •