Validate Form Data Before Submit

[Total: 0    Average: 0/5]

This shows another method to verify text boxes input and then put the focus into missing data boxes.

Head Code:

<script language="javascript"> 
function verify(form) { 
  for (i=0; i<form.elements.length; i++ ) { 
    form.elements[i].focus();
    if ((form.elements[i].type=="text" || 
         form.elements[i].type=="textarea") &&
		form.elements[i].value == "") 
    { 
	  // having this alert not commented out causes the focus to not work correctly...
      //alert("Please fill out all fields.");
      return;
    } 
  }
  // if all else is validated - then validiate the email...
  everify(form);
} 

function everify(form) { 
  if (form.email.value == "" || 
      form.email.value.indexOf('@') == -1 || 
      form.email.value.indexOf('.') == -1 || 
      form.email.value.length<6) { 
	// same for this alert and the focus issue.
    //alert("Not a valid e-mail address!");
	form.email.focus();
	return;
  } 
  // uncomment this to submit the form when all validated.
  // form.submit();
} 
</script> 

Body Code:

<form> 
<table  cellpadding=2 cellspacing=2 ><tbody  valign="top"> 
<tr><td>First Name<td><input type=text name="first" size=26></tr> 
<tr><td>Last Name<td><input type=text name="last"size=26></tr> 
<tr><td>Mailing Address<td><textarea name="address" rows=3 cols=20 ></textarea></tr> 

<tr><td>Day Time No.<td><input type=text  name="day" size=26></tr> 
<tr><td>Evening No.<td><input type=text name="evening" size=26></tr> 
<tr><td>Email Address<td><input type=text name="email" size=26></tr> 

</tbody></table> 
<p> 

<p> 
<input type="button" value="Submit" onClick="verify(this.form);"> 
<input type="Reset"  value="Reset"> 
</form> 

Times Viewed: 2

Leave a Reply

Your email address will not be published. Required fields are marked *