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

Validation Script

Status
Not open for further replies.

stfarm

Programmer
May 31, 2001
179
CA
I have a couple of questions. How can I validate check boxes, radio buttons and drop downs???? I hope you can help me with that. Thanks.

Steve Farmer


<SCRIPT LANGUAGE=&quot;JavaScript&quot; TYPE=&quot;text/javascript&quot;>
<!--

// validate the form area to make sure that they fill out all fields.
function VALIDATE_FORM_FIELDS() {

if (document.validate.man_name.value == &quot;&quot;) {
alert(&quot;Please enter your name.&quot;);
document.validate.man_name.focus();
return false;
}
else if (document.validate.man_email.value == &quot;&quot;) {
alert(&quot;Please enter a valid e-mail address.&quot;);
document.validate.man_email.focus();
return false;
}
else if (document.validate.man_phone.value == &quot;&quot;) {
alert(&quot;Please enter your phone number.&quot;);
document.validate.man_phone.focus();
return false;
}
else if (document.validate.emp_lname.value == &quot;&quot;) {
alert(&quot;Please enter the employees last name.&quot;);
document.validate.emp_lname.focus();
return false;
}
else if (document.validate.emp_fname.value == &quot;&quot;) {
alert(&quot;Please enter the employees first name.&quot;);
document.validate.emp_fname.focus();
return false;
}
else if (document.validate.emp_mname.value == &quot;&quot;) {
alert(&quot;Please enter the employees middle name.&quot;);
document.validate.emp_mname.focus();
return false;
}
else if (document.validate.job_address.value == &quot;&quot;) {
alert(&quot;Please enter the address.&quot;);
document.validate.job_address.focus();
return false;
}
else if (document.validate.job_city.value == &quot;&quot;) {
alert(&quot;Please enter the city.&quot;);
document.validate.job_city.focus();
return false;
}
else if (document.validate.job_state.value == &quot;&quot;) {
alert(&quot;Please enter the state.&quot;);
document.validate.job_state.focus();
return false;
}
else if (document.validate.job_zip.value == &quot;&quot;) {
alert(&quot;Please enter the zip.&quot;);
document.validate.job_zip.focus();
return false;
}


else {
// lets get crazy and have Art step in to check for a valid e-mail address
var email_pattern = /^(\w|-)+\@([a-zA-Z0-9]|-)+\.(\w|\.)+/;// checks for a word charachter or '-' one or more times followed by an '@' followed by [a-z] [A-Z] or [0-9] or '-' one or more times followed by a &quot;.&quot; followed by a word character or &quot;.&quot; one for more times.
var email_result;
email_result = email_pattern.exec(document.validate.man_email.value);
if (email_result) {
// good to go!
}
else {
alert(&quot;That is not a valid e-mail address.\nPlease try again.&quot;);
document.validate.man_email.focus();
return false;
}



// if everything passes, submit the form.
return true;
}

}//close function

//-->
</SCRIPT>
 
(i hope you realize it will be a real pain for users to fill you form, as they'll get warned one error by one error !!)

validating checkboxes : formname.checkboxename.checked (true if checked, false otherwise)
validating radiobuttons
same as checkboxes
validating drop down
formname.selectname.options[formname.selectedindex].value
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top