Free Web Hosting by Netfirms
Web Hosting by Netfirms | Free Domain Names by Netfirms

William's Utilities :: Validate Script



<title>Contact Webmaster</title><font style="font-size: 16pt;">Contact the Webmaster</font><p>
<script language="javascript">
// Validate() javascript function
// Generic form validation function. Pass all fieldnames that must be answered on the form.
// radio button: fails if none are selected
// text: fails if field is blank
// select-one: fails if topmost entry is selected
// returns true if form elements are okay, otherwise false
function Validate() {
   var strUserMessage;
   var bolResult=true; // assume form is okay
   var strField;
   var objFocusField=null;
   var objField;
   var bolRadioButtonSelected=false;
   var strFieldMessage="";
   // ensure all fields passed exist
   strUserMessage="Validate(): Error in form.\n\n";
   for (var intField=0; intField<arguments.length; intField+=2) {
      objField=arguments[intField];
      strFieldMessage=arguments[intField+1];
      if (!(objField)) {
         strUserMessage=strUserMessage+" Form element '"+strFieldMessage+"' does not exist.\n";
         bolResult=false;
      }
   }
   if (bolResult==false) {
      alert(strUserMessage);
      return bolResult;
   }
   strUserMessage="Please complete the following fields:\n\n";
   // prepare radio buttons
   for (var intFields=0; intFields<arguments.length; intFields+=2) {
      // assign radio button names so they can been "seen" more easily
      if (!arguments[intFields].name && arguments[intFields][0].type=="radio") {
         arguments[intFields].name=arguments[intFields][0].name;
         arguments[intFields].type=arguments[intFields][0].type;
      }
   }
   // iterate through field objects
   for (var intFields=0; intFields<arguments.length; intFields+=2) {
      objField=arguments[intFields];
      strFieldMessage=arguments[intFields+1];
      // if it's a radio button, iterate through all the options to see if one is checked
      if (objField.type=="radio") {
         bolRadioButtonSelected=false;
         // see if this radio button is checked
         for (var intRadioButtonOptions=0; intRadioButtonOptions<objField.length; intRadioButtonOptions++) {
            if (objField[intRadioButtonOptions].checked) {
               bolRadioButtonSelected=true;
            }
         }
         // if none of the radio buttons are checked, fail
         if (bolRadioButtonSelected==false) {
            bolResult=false;
            strUserMessage+=" "+strFieldMessage+"\n";
            if (objFocusField==null) { objFocusField=objField[0]; }
         }
      // if the text field is empty, fail
      } else if (objField.type=="text" || objField.type=="password" || objField.type=="textarea" || objField.type=="file") {
         if (objField.value=="") {
            bolResult=false;
            strUserMessage+=" "+strFieldMessage+"\n";
            if (objFocusField==null) { objFocusField=objField; }
         }
      // if the checkbox is empty, fail
      } else if (objField.type=="checkbox") {
         if (objField.checked==false) {
            bolResult=false;
            strUserMessage+=" "+strFieldMessage+"\n";
            if (objFocusField==null) { objFocusField=objField; }
         }
      // if the top item is selected, fail
      } else if (objField.type=="select-one") {
         if (objField.selectedIndex<1 && objField.size<=1) {
            bolResult=false;
            strUserMessage+=" "+strFieldMessage+"\n";
            if (objFocusField==null) { objFocusField=objField; }
         }
         if (objField.size>1 && objField.value=="") {
            bolResult=false;
            strUserMessage+=" "+strFieldMessage+"\n";
            if (objFocusField==null) { objFocusField=objField; }
         }
      }
   }

   // if form isn't complete yet, put cursor on 1st blank entry
   if (objFocusField!=null) objFocusField.focus();
   if (bolResult==false) {
      alert(strUserMessage);
   }
   return bolResult;
}

</script>
<form method="post" action="email-webmaster.asp" onsubmit="javascript: return Validate(fromname, 'Your name', fromaddress, 'Your email address', subject, 'Subject', body, 'Message')">

<table border="0" cellspacing="1" cellpadding="1">
<tr>
<td>Your Name:</td>
<td>
<input type="text" name="fromname" maxlength="64" size="20">
</td>
</tr>
<tr>
<td>Your E-mail Address:</td>
<td>
<input type="text" name="fromaddress" size="20" maxlength="64">
</td>
</tr>
<tr>
<td>Subject:</td>
<td>
<select name="subject">
<option value="- Please Choose one -" selected>- Please Choose One -</option>
<option value="I have a suggestion">I have a Suggestion</option>
<option value="I had a problem finding something">I had a problem finding something</option>
<option value="I found an error">I found an error</option>
<option value="Other">Other</option>
</select>
</td>
</tr>
<tr>
<td>Message:</td>
<td>
<textarea rows="7" name="body" cols="45"></textarea>
</td>
</tr>
</table>
<p align="center">
<input type="submit" name="submit" value="Send">

</form>