Validate Form Data Onsubmit

[Total: 0    Average: 0/5]

This example shows how to have a simple form validated before submitting.

Head Code:

<script language="javascript"> 

<!-- Begin 
function check(which) { 
  var p=true; 
  if (document.images) { 
    for (i=0;i<which.length;i++) { 
      var tempobj=which.elements[i]; 
      if (tempobj.name.substring(0,8)=="") { 
        if (((tempobj.type=="text" ||
		      tempobj.type=="textarea")&& 
             tempobj.value=='') ||
			(tempobj.type.toString().charAt(0)=="s"&& 
             tempobj.selectedIndex==0)) { 
          p=false; 
          break; 
        } 
      } 
    } 
  } 
  if (!p) { 
    shortFieldName=tempobj.name.substring(8,30).toUpperCase(); 
    alert("Please make sure the "+shortFieldName+" field was properly completed."); 
    return false; 
  } 
  else 
    return true; 
} 
//  End --> 

</script> 

Body Code:

<center> 
<form onSubmit="return check(this)"> 

<input type="text" > 
<br> 
<input type="text" name="email"> 
<br> 
<select name="hobby"> 
<option selected>Pick an option! 
<option>1 
<option>2 
<option>3 
</select> 
<br> 
<textarea name="comments"></textarea> 
<br> 
<input type=submit value="Submit"> 
</form> 
</center> 

Times Viewed: 0

Leave a Reply

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