...

View Full Version : Not submitting when validation is true



StitchFL
07-21-2009, 07:07 PM
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:

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

onclick="check_submit();check_paypal();return validate()"

Help or suggestions would be much appreciated. :)

ckeyrouz
07-21-2009, 07:10 PM
I would rather do it this way:


onclick=""



function checkAndSubmit()
{
check_submit();
check_paypal();
if(validate())
{
document.formc.submit();

}
}

StitchFL
07-21-2009, 07:40 PM
Thanks for the suggestion.

So on your suggestion would it be...


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. :)

Philip M
07-21-2009, 08:12 PM
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.

StitchFL
07-21-2009, 08:18 PM
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?

ckeyrouz
07-21-2009, 08:20 PM
Philip M is right but I think I forgot something in my previous post:


onclick="checkAndSubmit()"

StitchFL
07-21-2009, 08:29 PM
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.

ckeyrouz
07-21-2009, 08:37 PM
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-friendly-resources/dom-scripting/validate-forms-javascript.shtml

StitchFL
07-21-2009, 08:41 PM
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

function checkAndSubmit()
{
if(validate() == true)
{
check_submit();
check_paypal();
}
}

Philip M
07-21-2009, 08:51 PM
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:-


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.



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;
}

StitchFL
07-22-2009, 04:49 AM
Thank you ckeyrouz for your helpful suggestion and your patience.

Thanks for the code suggestions Phillip M.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum