ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel
  • »
  • Technology»
  • Computers & Software»
  • Computer Science & Programming»
  • Programming Languages

Javascript: Simple Form Validation

Updated on September 12, 2012

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.

Click to Rate This Article