JavaScript are mainly used to make WebPages more attractive by applying effects like Mouseover, Statusbar, Scrollbar, Onfocus, Onselect etc. 

But one of the major applications which has come in to existence recently is Form Validations. 

On the Internet we come across a lot of form to be filled by us like for e.g. registration for email account, or adding you name in some contest or games, or apply for some loan online. For all these you fill up a form, which is of great value, because whatever you enter it becomes your profile or your record on the site.

It may happen that due to human error while typing you tend to enter wrong spellings or some other mistakes (e.g. for Bansal you might enter Bansam) so your name changes.

Here if we have a field of entering of Email Address, a person might forget to enter @xyz.com and might enter only the his username, so here we need to remind the user that he has entered wrong Email id and has to enter the right one.

Similarly it may happen that a user might enter his email Id in the field where phone no. has to be entered. Now here a phone no. field is supposed to contain only integers and not characters, so we could restrict the field to contain only integers so that if any character or any other symbol than integer is entered, you get a dialog box saying that you are not supposed to enter characters in this field. 

Finally when the form is properly validated, there are least chances of error to occur and we can say that the form is serving its purpose.

Simple validation script:

<html>
<script>
function test(myform)
{
if (myform.elements[0].value.length <= 0 )
      alert("Please Insert Your Name");
else  
       {
       alert(" Thank You ");
        }
}
</script>
<body>
<input type="text" >
<input type="button" value="Enter your name" Onclick = "test(this.form)">
</body>
</html>

  


A Script to find if the entered email id contains ' @ '  character OR

Script ensure that there are no integers in a field that should contain only characters

 

Back