JavaScript Tip of the Week for September 9, 1996: Forms Validation | Source Code | WebReference

JavaScript Tip of the Week for September 9, 1996: Forms Validation | Source Code

JavaScript Tip of the Weekfor September 9, 1996: Source Code: Forms Validation


Put the onSubmit event handler into the form tag of a given form:
        <FORM NAME = "theform" onSubmit="return formCheck()">
Use a function like this, activated by onSubmit, to detect errors in a given form:
/* This code is Copyright (c) 1996 Nick Heinle and Athenia Associates, 
 * all rights reserved. In order to receive the right to license this 
 * code for use on your site the original code must be copied from the
 * Web site webreference.com/javascript/. License is granted to user to 
 * reuse this code on their own Web site if and only if this entire copyright
 * notice is included. Code written by Nick Heinle of webreference.com.
 */
function formCheck() 
{
        if (document.theform.email.value.indexOf("@") == -1 ||
            document.theform.email.value == "") 
        {
        alert("Please include a proper email address.");
        return false;
        }
       
        if (document.theform.user_name.value == "") 
        {
        alert("Please put in a name.");
        return false;
        }
}