in reply to How do I detect and handle empty form fields?

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Untitled Document</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <script language="JavaScript" type="text/javascript"> //function to check empty fields function isEmpty(textfield1, textfield2, textfield3) { //change "field1, field2 and field3" to your field names textfield1 = document.forms1.field1.value textfield2 = document.forms1.field2.value textfield3 = document.forms1.field3.value //name field if (textfield1 == "" || textfield1 == null || !isNaN(textfield1) || textfield1.charAt(0) == ' ') { alert("\"Field 1\" is a mandatory field.\nPlease amend and retry.") return false; } //url field if (textfield2 == "" || textfield2 == null || textfield2.charAt(0) == ' ') { alert("\"Field 2\" is a mandatory field.\nPlease amend and retry.") return false; } //title field if (textfield3 == "" || textfield3 == null || textfield3.charAt(0) == ' ') { alert("\"Field 3\" is a mandatory field.\nPlease amend and retry.") return false; } return true; } </script> <body> <form name="form1" method="post" action="">
Name : <input type="text" name="textfield">
Address : <input type="text" name="textfield2">
Phone : <input type="text" name="textfield3">
E-mail : <input type="text" name="textfield4">
<input type="submit" name="Submit" value="Submit"> <script language="JavaScript" type="text/javascript">
<input type="reset" name="Submit2" value="Reset">
</form> </body> </html>

Originally posted as a Categorized Answer.

  • Comment on Re: How do I detect and handle empty form fields?