Click to See Complete Forum and Search --> : Validating Form Error


96turnerri
11-29-2003, 10:39 AM
hi i have made this script which validates my form, it validates ok but when correction has been made to a missed field on the form, so it should now pass it doesnt :confused: can anyone see whats wrong with this code? thanks 96


<script type="text/javascript">
function validate() {
if (document.quotation.Name.value.length < 5) {
alert("Please Enter Your Full Name.");
document.quotation.Name.focus();
return false;
}
if (document.quotation.Email.value.length < 10) {
alert("Please Enter Your Full Email Address.");
document.quotation.Email.focus();
return false;
}
if (document.quotation.Domain[0].checked) {
if (document.quotation.DomainName.value.length < 10);
alert("Please Enter An Example Of A Domain Name.");
document.quotation.DomainName.focus();
return false;
}
if (document.quotation.ExistingSite[0].checked) {
if (document.quotation.ExistingURL.value.length < 15);
alert("Please Enter Your Existing Sites URL.");
document.quotation.ExistingURL.focus();
return false;
}
if (document.quotation.Updates[0].checked) {
if (document.quotation.Update.options == document.quotation.Update.defaultSelected);
alert("Please Choose Update Frequency.");
document.quotation.Update.options.focus();
return false;
}
if (document.quotation.LogoColour[1].checked) {
if (document.quotation.ColoursLogo.options == document.quotation.ColoursLogo.defaultSelected);
alert("Please Choose Company Colour / Logo Design Type.");
document.quotation.ColoursLogo.options.focus();
return false;
}
if (document.quotation.Forms.checked) {
if (document.quotation.forms.options == document.quotation.forms.defaultSelected);
alert("Please Select The Number Of Forms You Require.");
document.quotation.forms.options.focus();
return false;
}
return true;
}
</script>

96turnerri
11-30-2003, 02:47 PM
*bump* gone off 2nd page unanswered :(

pyro
11-30-2003, 03:24 PM
How are you calling the validate function?

96turnerri
12-01-2003, 01:56 PM
i am calling it by <form .... onSubmit="return validate();">

Thanks for your help pyro

pyro
12-01-2003, 02:20 PM
Instead of using all if() statments, use if, else if, and else.

Something like this:

function validate() {
if (something != "something") {
alert ("something");
return false;
}
else if (somethingelse != "somethingelse") {
alert ("something");
return false;
}
else {
return true;
}
}

fredmv
12-01-2003, 03:08 PM
Use Peter Bailey's fValidate (http://www.peterbailey.net/fValidate/). ;)

96turnerri
12-01-2003, 03:14 PM
thanks i think i understand what you saying but could you give me an exmaple with say this bit


if (document.quotation.Domain[0].checked) {
if (document.quotation.DomainName.value.length < 10);
alert("Please Enter An Example Of A Domain Name.");
document.quotation.DomainName.focus();
return false;
}

96turnerri
12-01-2003, 03:33 PM
right i managed to get the 3rd and 4th ones working just the last 3 which are all the same which do not work at the moment, thanks for your help

hers my new code


<script type="text/javascript">
function validate() {
if (document.quotation.Name.value.length < 5) {
alert("Please Enter Your Full Name.");
document.quotation.Name.focus();
return false;
}
if (document.quotation.Email.value.length < 10) {
alert("Please Enter Your Full Email Address.");
document.quotation.Email.focus();
return false;
}
if (document.quotation.Domain[0].checked) {
if (document.quotation.DomainName.value.length < 12) {
alert ("Please Enter An Example Of A Domain Name You Would Like");
document.quotation.DomainName.focus();
return false;
}
}
if (document.quotation.ExistingSite[0].checked) {
if (document.quotation.ExistingURL.value.length < 12) {
alert("Please Enter Your Existing Sites URL.");
document.quotation.ExistingURL.focus();
return false;
}
}
if (document.quotation.Updates[0].checked) {
if (document.quotation.Update.options == document.quotation.Update.defaultSelected);
alert("Please Choose Update Frequency.");
document.quotation.Update.options.focus();
return false;
}
if (document.quotation.LogoColour[1].checked) {
if (document.quotation.ColoursLogo.options == document.quotation.ColoursLogo.defaultSelected);
alert("Please Choose Company Colour / Logo Design Type.");
document.quotation.ColoursLogo.options.focus();
return false;
}
if (document.quotation.Forms.checked) {
if (document.quotation.forms.options == document.quotation.forms.defaultSelected);
alert("Please Select The Number Of Forms You Require.");
document.quotation.forms.options.focus();
return false;
}
return true;
}
</script>

96

pyro
12-03-2003, 05:22 PM
You need to use the if, else if, else syntax that I demonstrated above...

96turnerri
12-04-2003, 11:37 AM
i tried it that way but couldnt get it to work, wondering if you could give me an example on this one cos the last 3 are all same


if (document.quotation.LogoColour[1].checked) {
if (document.quotation.ColoursLogo.options == document.quotation.ColoursLogo.defaultSelected);
alert("Please Choose Company Colour / Logo Design Type.");
document.quotation.ColoursLogo.options.focus();
return false;
}


thanks
96