Client Side Form Validation – using JavaScript

By | March 1, 2011

Hi, JavaScript is mainly used for client side scripting. In client side scripting one of the thing which we are usually in need is Validation of Forms. We want to know the user , who is interacting with the form have already given the necessary details before submitting or saving etc. Here is a simple form validation example using JavaScript where the form is checked for a particular text field have entered or not. Here is the form code

First name:

It contains a Text field to enter first name and a button to save.
On clicking the Save button it will go for ‘validateForm()’ function and checks for user entry.

validateForm() code

function validateForm()
var x=document.forms["trialForm"]["firstName"].value
if (x==null || x=="")
  alert("Enter your first name before saving!");
  return false;

This function checks for it & returns alert if it is not filled properly! 🙂

Leave a Reply

Your email address will not be published.