Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Syntax errors in form validation

Status
Not open for further replies.

gcrighton

Technical User
Feb 23, 2002
9
ES
Hi,

BUG 1.

I'm using the following code to validate a group of 4 radio buttons. The validation works but when "submit" is finally entered after all validation a pesky taskbar error displays before the user is taken to the next page. It appears to be syntax related (I think). Any ideas anyone?

if (document.application.cuota[0].checked == false && document.application.cuota[1].checked == false && document.application.cuota[2].checked == false && document.application.cuota[3].checked == false) {
alert (error9);
return false;
}

BUG 2

Another related syntax error in a non-standard statement (Jeez, try saying that when your hissed!) is living here and out of my sight:

if (lang != "en" && document.application.nif.value == "") {
alert(error6);
document.application.nif.select();
return false;
}

Explanation: the site is in two languages with the same validation script shared. This particular field is *not* on the English version of the site, so the check should be "if the language is not English and the field nif is blank, show an error". Like the above it validates but gives a taskbar error. It's not related to the language issue as this works for all other standard fields.

Can anyone point out the bugs in my lousy syntax?

Thanks in advance and all advice appreciated.

Gary
 
hi gcrighton,
First of all, you should post your entire code, otherwise it's often impossible to figure out what's up.

But before you do this, try the following:
add an additional pair of ( ) to every comparison statement:

if ( (lang != "en") && (document.application.nif.value == "") )

Also, I'd rather change the code for BUG 1 to this:

if ( (!document.application.cuota[0].checked) &&
(!document.application.cuota[1].checked) &&
(!document.application.cuota[2].checked ) &&
(!document.application.cuota[3].checked) )

Check this and tell the result.
 
Hi Starway,

Thanks for your comments - the validation which included the language check no longer causes a problem with the extra ()'s included.

The radio check still generates the taskbar error (although as before it does in fact perform the validation). The error is clearly pinpointed here as I've run tests with this section of code commented out and the error disappears.

Additional information: the form spans two pages with the radio buttons located on form two. Both forms take the same name (application) and call the same validation script. The taskbar error is generated when next / submit is clicked on either page.

I've included full code below as you suggest.

Thanks for your help!

Gary


How the script is called:

<form name=&quot;application&quot; action=&quot;/production/socio/suscribase2.php?lang=<?PHP echo $lang?>&quot; method=&quot;post&quot; onSubmit=&quot;return validate1();&quot;>


The submit button (page 1):

<input type=&quot;submit&quot; value=&quot;<?php if ($lang == &quot;en&quot;) { ?> Next <? } else { ?> Siguiente <? } ?>&quot;>


The validation script:

<!--

if (lang == &quot;en&quot;) {

var error1 = &quot;Please enter a name&quot;;
var error2 = &quot;Please enter a street address&quot;;
var error3 = &quot;Please enter a postcode&quot;;
var error4 = &quot;Please enter a town&quot;;
var error5 = &quot;Please enter a State / County:&quot;;
var error7 = &quot;You must provide either a telephone number or an email address&quot;;
var error8 = &quot;The email address is invalid&quot;;
var error9 = &quot;You have not selected a membership type&quot;; // Radio buttons
var error10 = &quot;Please enter your name&quot;; // Difusion por amistad
var error11 = &quot;Please enter your membership number&quot;; // Difusion por amistad
var error12 = &quot;Please enter your friends name&quot;; // Difusion por amistad

} else {

var error1 = &quot;ES Please enter a name&quot;;
var error2 = &quot;ES Please enter a street address&quot;;
var error3 = &quot;ES Please enter a postcode&quot;;
var error4 = &quot;ES Please enter a town&quot;;
var error5 = &quot;ES Please enter a State / County:&quot;;
var error6 = &quot;ES Please enter an NIF&quot;;
var error7 = &quot;ES You must provide either a telephone number or an email address&quot;;
var error8 = &quot;ES The email address is invalid&quot;;
var error9 = &quot;ES You have not selected a membership type&quot;; // Radio buttons
var error10 = &quot;ES Please enter your name&quot;; // Difusion por amistad
var error11 = &quot;ES Please enter your membership number&quot;; // Difusion por amistad
var error12 = &quot;ES Please enter your friends name&quot;; // Difusion por amistad

}

//SOLICITUD DE APLICACION
function validate1(){

if(document.application.name.value == &quot;&quot;) {
alert(error1);
document.application.name.select();
return false;
} else

if(document.application.direccion.value == &quot;&quot;) {
alert(error2);
document.application.direccion.select();
return false;
}

if(document.application.codigo_postal.value == &quot;&quot;) {
alert(error3);
document.application.codigo_postal.select();
return false;
}

if(document.application.poblacion.value == &quot;&quot;) {
alert(error4);
document.application.poblacion.select();
return false;
}

if(document.application.provincia.value == &quot;&quot;) {
alert(error5);
document.application.provincia.select();
return false;
}

if ( (lang != &quot;en&quot;) && (document.application.nif.value == &quot;&quot;) ) {
alert(error6);
document.application.nif.select();
return false;
}

//MAKING EITHER TELEPHONE OR EMAIL MANDATORY
if (document.application.telefono.value == &quot;&quot; && document.application.email.value == &quot;&quot;) {
alert (error7);
document.application.telefono.select();
return false;
}

//BUT, if AN EMAIL ADDRESS IS ENTERED IT SHOULD BE CHECKED
if ((document.application.email.value != &quot;&quot;) && (document.application.email.value.indexOf ('@',0) == -1 || document.application.email.value.indexOf ('.',0) == -1)) {
alert (error8);
document.application.email.select();
return false;
}

//ORIGINAL SYNTAX
//if (document.application.cuota[0].checked == false && document.application.cuota[1].checked == false && document.application.cuota[2].checked == false && document.application.cuota[3].checked == false) {
//alert (error9);
//return false;
//}

//SUGGESTED SYNTAX
if ((!document.application.cuota[0].checked) &&
(!document.application.cuota[1].checked) &&
(!document.application.cuota[2].checked) &&
(!document.application.cuota[3].checked)) {
alert (error9);
return false;
}

return true;

}
 
Another suggestion:
always add else between if statements:

if (...) {
}
else

if (...) {
}
else

if (...) {
}

This can avoid problems with multiple if's. Don't forget that you shouldn't put &quot;;&quot; before else.


You didn't show the html code for the form containing radio buttons. We have to see everyhting in order to realize what's going on.
Please post here the entire page (html+scripts) if it's not too long (or cut out unnecessary parts), so it's possible to copy/paste it -- this will save time.

btw, what error do you get?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top