Javascript: Simple Form Validation

Are you looking for a simple client side form field validation for your web page form?

Form validation is needed with all forms that are submitted online. You can have client side or server side validation. Server side is a better solution, because all the work is done on the server side. You have more control and more flexibility on how the validation is done and to what extint. It is more secure than client side.

Client side validation is a much faster set-up and a quicker response back to the user. However, it is not as secure as the server side version. It's still a good deterent for not allowing the user to submit a blank form, or invalid data format. Client side validation, is usually a javascript embedded into the web page itself.

Blank Form Field Validation

Below is a simple example of a form field validation, when the field is left blank.

Javascript Code

Place the following javascript code between the <head></head> tags of your web page.

<script type="text/javascript">

function validate_form ( )
    valid = true;
    if ( document.contact_form.contact_email.value == "" )
        alert ( "Please provide a valid 'Email'" );
        valid = false;

    return valid;


HTML Sample Code

Below is a sample of the <HTML> code that can be placed any where inside your web page.

<form name="contact_form" method="post" action="sendform.html"
onsubmit="return validate_form ( );">
Email: <input type="text" name="contact_email">
<input type="submit" name="Submit" value="Submit">

Other Javascripts

Below are some links to other javascript articles.

More by this Author

  • How to Install a Ceiling Fan

    When installing a home ceiling fan you want to make sure that your follow several safety tips and warnings so that you prevent fire hazards, electrical shock, and personal injury. Read carefully when looking over the...

  • How to Get Rid of Mice In and Around The House?

    Until this year, the only time that I've had mice or rats in the house were when my daughter had pet mice and rats, and one of those times we did have one loose in the house. (Science fair project got loose.) Anyway,...

  • How to Build Wooden Roof Trusses

    You may refer to a truss as the rafter, but it's basically the skeleton of the roof, carrying the weight of the frame and supporting the walls of the building. Trusses are very important to preventing the walls from...

Click to Rate This Article