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;
}

//-->
</script>

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">
<p>
<input type="submit" name="Submit" value="Submit">
</form>

Other Javascripts

Below are some links to other javascript articles.

More by this Author

  • Tips For How to Stain Concrete Floors
    1

    You don't always have to tile, carpet, or hardwood your floors. The new thing is to just stain the concrete that's already there. It's a lot of work, but if done properly, your home will look gorgeous. If you're...

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

    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
    21

    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
working