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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

conditional validation of form elements 1

Status
Not open for further replies.

crusader

Programmer
Apr 26, 2001
24
GB
Hi
Can anyone tell me how to validate a form where there are several different form elements of which I want to have a "cannot be empty" validation on groups of form fields.
e.g.
f a user enters information into one field (say name in a text field) I want to make sure they fill in certain other related fields for example age and address but dont want to enforce the validation on all of my fields in the rest of the form.

<form method=&quot;post&quot; action=&quot;nextpage.htm&quot;>
<input type=&quot;text&quot; name=&quot;username&quot;>
<input type=&quot;text&quot; name=&quot;useradd&quot;>
<input type=&quot;text&quot; name=&quot;useradd&quot;>
<!-- the 3 above to be related in validation if one is filled in then all 3 must but not 2 below-->

<input type=&quot;text&quot; name=&quot;driver&quot;>
<input type=&quot;text&quot; name=&quot;carmake&quot;>
<!-- the 2 above to be related in validation if one is filled in then other must but not first 3-->

</form>

Any ideas?
Thanks,
Crusader...
 
This should give you a start... there are easier options if you standardize the field names...

<script>
function checkForm(){
for (x=0; x<document.myForm.elements.length; x++){
thisName = document.myForm.elements[x].name
thisValue = document.myForm.elements[x].value
if (thisName.substr(0,4) == &quot;user&quot; && thisValue != &quot;&quot;){
if (document.myForm.username.value == &quot;&quot; || document.myForm.useradd.value == &quot;&quot; || document.myForm.userphone.value == &quot;&quot;){
alert(&quot;please complete user info&quot;)
return false;
}
}
}
}
</script> Get the Best Answers! faq333-2924
Is this an asp FAQ? faq333-3048
Tek-Tips Best Practices: FAQ183-3179
 
Thanks mwolf00!

Thats perfect.

cheers,
Crusader.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top