Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    New to the CF scene
    Join Date
    Jul 2009
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Not submitting when validation is true

    I've setup a form on my website and added a simple validation script to check a few of the fields. After all the validations are done I want the submit button to process two other functions that will handle the submission of the form.

    The problem is that if the validation returns true the script just stops and doesn't run the other two functions.

    Here is my validation script:
    Code:
     function validate()
      {
       var email=document.formc.Email.value
       var name=document.formc.Exhib_Name.value
       var entries=document.formc.p1D40.value
       var paym=document.formc.PayMeth.value
      if (email==null||email=="")
        {
        alert("Please enter an email address");
    	return false;
        }
      else if (name==null||name=="")
        {
    	alert("Please enter your name.");
    	return false;
    	}
       else if (entries==null||entries==0)
        {
    	alert("You have no entries, please check the form and re-submit.");
    	return false;
    	}
    	else if (paym==1)
    	{
    	alert("Please choose a Payment Method");
    	return false;
    	}
    	else 
    	{
        return true;
        }
      }
    Then on my submit button I have
    Code:
    onclick="check_submit();check_paypal();return validate()"
    Help or suggestions would be much appreciated.

  • #2
    Senior Coder ckeyrouz's Avatar
    Join Date
    Jun 2009
    Location
    Montreal, Canada
    Posts
    1,044
    Thanks
    5
    Thanked 179 Times in 179 Posts
    I would rather do it this way:

    Code:
    onclick=""
    Code:
    function checkAndSubmit()
    {
      check_submit();
      check_paypal();
      if(validate())
      {
         document.formc.submit();
    
      }
    }

  • Users who have thanked ckeyrouz for this post:

    StitchFL (07-21-2009)

  • #3
    New to the CF scene
    Join Date
    Jul 2009
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Thanks for the suggestion.

    So on your suggestion would it be...

    Code:
    function checkAndSubmit()
    {
      check_submit();
      check_paypal();
      if(validate()=true)
      {
         document.formc.submit();
    
      }
    }
    I tried to implement it as your example shows but if I purposely leave a validate field blank, it still submits the form once I hit 'ok' on the alert box. I want it to stop the script once 'ok' is clicked on alert box so the user can fix the form then try to submit again.

    Hope you'll bear with me, I'm learning this as I'm building it.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Quote Originally Posted by StitchFL View Post
    Code:
    function checkAndSubmit()
    {
      check_submit();
      check_paypal();
      if(validate())  {  // or if (validate() == true)
        document.formc.submit();
    
      }
    else {return false};
    }
    Validation of the kind if (email==null||email=="") is barely worthy of the name, and almost worthless, as even a single space or a ? will return false (i.e. pass the validation).

    You shoud avoid giving the name name to HTMl elements or Javascript variables, as this can throw the browser under certain circumstances. Use something like username instead. Again, do not use exactly the same name for the HTML form element and the Javascript variable.
    Last edited by Philip M; 07-21-2009 at 07:20 PM.

  • Users who have thanked Philip M for this post:

    StitchFL (07-21-2009)

  • #5
    New to the CF scene
    Join Date
    Jul 2009
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts
    I tried the '==' with the same result.

    I hear what you are saying about the validation and the variable name, do you have a suggestion you can point me in the direction in for a better validation script?

  • #6
    Senior Coder ckeyrouz's Avatar
    Join Date
    Jun 2009
    Location
    Montreal, Canada
    Posts
    1,044
    Thanks
    5
    Thanked 179 Times in 179 Posts
    Philip M is right but I think I forgot something in my previous post:

    Code:
    onclick="checkAndSubmit()"

  • #7
    New to the CF scene
    Join Date
    Jul 2009
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts
    I had that call on my submit button, thanks.

    I still have the issue that if the validate is false, it alerts me, I hit ok, but it still submits the form. If I can get this to work then I will try to do something more complicated than just checking for a blank space.

  • #8
    Senior Coder ckeyrouz's Avatar
    Join Date
    Jun 2009
    Location
    Montreal, Canada
    Posts
    1,044
    Thanks
    5
    Thanked 179 Times in 179 Posts
    I suggest that you take a look at this web site, it contains some good validation methods and in a smart way:

    http://www.webcredible.co.uk/user-fr...vascript.shtml

  • Users who have thanked ckeyrouz for this post:

    StitchFL (07-21-2009)

  • #9
    New to the CF scene
    Join Date
    Jul 2009
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts
    I got it to work, thanks for your help everyone.

    I'll work on checking for more than a blank field.

    Here is what worked
    Code:
    function checkAndSubmit()
    {
        if(validate() == true)
      {
         check_submit();
      	 check_paypal();
      }
    }

  • #10
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Quote Originally Posted by StitchFL View Post
    I tried the '==' with the same result.

    I hear what you are saying about the validation and the variable name, do you have a suggestion you can point me in the direction in for a better validation script?
    There are a zillion examples on this forum - use the search facility. Here is an excellent email validation test:-

    Code:
    if (!(/^([a-z0-9])([\w\.\-\+])+([a-z0-9])\@(([\w\-]?)+\.)+([a-z]{2,4})$/i.test(email))) { 
    alert ("You must enter a valid email address");
    document.formc.Email.value = "";  // clear the field
    document.formc.Email.focus();  // and re-focus on it
    return false;
    }
    Is the name field supposed to be surname, first name, both, or what?
    A name can consist only of the letters a-z plus hyphen, apostrophe and space.

    Code:
    username = username.replace(/^\s+|\s+$/g,"");  // strip leading and trailing spaces
    if ((/[^a-zA-Z\s\-\']/g.test(username)) || (username.length <2)) {  // if a full name is required increase 2 to say 6
    alert ("Please enter your name!");
    document.formc.Uname.value =  "";  // clear the field
    document.formc.Uname.focus();  // and re-focus on it
    return false;
    }
    Last edited by Philip M; 07-21-2009 at 08:44 PM.

  • #11
    New to the CF scene
    Join Date
    Jul 2009
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Thank you ckeyrouz for your helpful suggestion and your patience.

    Thanks for the code suggestions Phillip M.


  •  

    Posting Permissions

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