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

Compulsory form fields 1

Status
Not open for further replies.

meeble

Programmer
Sep 24, 2002
137
GB
I have three fields in my form:

Home telephone
Work telephone
Mobile telephone

I want to make it compulsory that a user fills in at least any ONE of these boxes.

How do you do this?

Cheers

James
 
You set up a Javascript function that tests the values and either accepts or rejects the submission of the form. Here is a possible function you could use in Javascript:

Code:
<script type="text/javascript>
function checkSubmit()
{
 var myObj = document.myFormName;
 phoneCount = 0;
 if (myObj.homePhone.value != "") phoneCount++;
 if (myObj.workPhone.value != "") phoneCount++;
 if (myObj.mobilePhone.value != "") phoneCount++;

 if (phoneCount == 0)
 {
  alert('Please supply a phone number for us to contact you.');
  return false;
 }
}
</script>

This assumes a form setup like the following:

Code:
<form name="myFormName" method="post" action="somefile.cgi" onsubmit="return(checkSubmit())">
<input type="text" value="" name="homePhone" /><br />
<input type="text" value="" name="workPhone" /><br />
<input type="text" value="" name="mobilePhone" /><br />
<input type="submit" value="Send form" name="submitButton" />
</form>

Hope this gets you started.

Jeff
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top