By A Web Design
The first validation check on form fields is always the empty field check. There really is little sense in testing the contents of a form field if it’s holding nothing. Having said that, there are other checks which can be performed on form fields.
In this example we are checking if characters have been inserted in a telephone number, form field. In this specific instance the telephone number field is expected to hold only numeric values.
This example displays a form that requires a Telephone number form field to accept only numeric values before its dispatch to a Web server for further processing. The look and feel of the form is as shown in diagram 1.

Diagram1
The first snippet of code described is the HTML responsible for rendering the form in the visitor’s browser as shown in diagram 1.
If the telephone number is filled with non-numeric values and the Submit button is clicked an Alert with an appropriate error message will be displayed as shown in diagram 2.

Diagram2
<form name="frmTest" id="frmTest" method="post" action="" onsubmit="return validateTest(document.frmTest);"> <table id="tblTest" width="100%"> <tr> <td>Telephone</td> <td><input type="text" name="txtTelephone" id="txtTelephone" value="" title="Telephone" /></td> </tr> <tr> <td><input type="submit" name="btnSubmit" id="btnSubmit" value="Submit" /></td> <td><input type="reset" name="btnCancel" id="btnCancel" value="Cancel" /></td> </tr> </table> </form>
Elements used in the HTML code snippet are:
Description of each element
In the form frmTest the Javascript function validateTest(document.frmTest) is called when the Submit button is clicked. This is how that happens:
When the Submit button is clicked, the frmTest onsubmit method is automatically invoked which in turn invokes the Javascript function validateTest(); The form is passed to this Javascript function as the parameter - document.frmTest. Hence the function validateTest(); operates on document.frmTest.
It’s the signature of validateTest(); that tests if txtTelephone is left empty or not.
<script type="text/javascript"> function validateTest(objFrm) { if(objFrm.txtTelephone.value=="") //checking if the textbox txtTelephone is empty { alert("Telephone number cannot be left blank"); // An alert box is popped up in the browser if it is with a message //in it objFrm.txtTelephone.focus(); //Once the user responds to the alert the cursor is placed in the //textbox again return false; //The value false is returned to the function so that form //submission } else if(isNaN(objFrm.txtTelephone.value)) //checking if the textbox txtTelephone is contain only numerical value { alert("Telephone number should only contain numeric values"); // An alert box is popped up in the browser if it is with a message //in it objFrm.txtTelephone.focus(); //Once the user responds to the alert the cursor is placed in the //textbox again return false; //The value false is returned to the function so that //form submission is aborted } else { return true; // The value true is returned to the function so that form //submission goes through } } </script>
NOTE: While Javascript code can be included anywhere in the HTML codespec it is prudent that Javascript is always written within the <HEAD></HEAD> HTML tags.
To separate Javascript code from HTML code it is always placed within the <script></script> that are normally placed within <HEAD></HEAD> HTML tags.
The <SCRIPT> tag has following attributes:
type: It specifies MIME type of script
src: Specifies the URL of an external Javascript file if one is used
Working of the Javascript code:
if(objFrm.txtTelephone.value=="")
alert("Telephone number cannot be left blank");
objFrm.txtTelephone.focus();
else if(isNaN(objFrm.txtTelephone.value))
alert("Telephone number should only contain numeric values");
objFrm.txtTelephone.focus();
NOTE: In this specific case nothing else will happen because the form’s ACTION attribute is loaded with the # symbol. Normally value passed to the ACTION attribute of the form is the name of the program at the Web server that will process the data submitted by the form to the Web server. In this case since the ACTION attribute holds # the Web server will receive the data submitted by the form by will do nothing further.
<html> <head> <title> Check Valid Number </title> <script type="text/javascript" language="javascript"> function validateTest(objFrm) { if(objFrm.txtTelephone.value=="") { alert("Password cannot be left blank"); objFrm.txtPassword.value=""; objFrm.txtPassword.focus(); return false; } else if(isNaN(objFrm.txtTelephone.value)) { alert("Telephone number should only contain numeric"); objFrm.txtTelephone.value=""; objFrm.txtTelephone.focus(); return false; } else { return true; } } </script> </head> <body> <form name="frmTest" id="frmTest" method="post" action="" onsubmit="return validateTest(document.frmTest);"> <table id="tblTest" cellpadding="0" cellspacing="0" > <tr> 5 © Open Source Varsity. All Rights Reserved. <td>Telephone</td> <td> <input type="text" name="txtTelephone" id="txtTelephone" value="" title="Telephone" /> </td> </tr> <tr> <td><input type="submit" name="btnSubmit" id="btnSubmit" value="Submit" /></td> <td><input type="reset" name="btnCancel" id="btnCancel" value="Cancel" /></td> </tr> </table> </form> </body> </html>