// A utility function that returns true if a string contains only 
// whitespace characters
function isBlank(s) {
	for(var i=0; i < s.length; i++) {
		var c = s.charAt(i);
		if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
	}
	return true;
}

<!-- Changes:  Sandeep V. Tamhankar (stamhankar@hotmail.com) -->

/* 	1.2:  Altered function to work within verify(). Allows blank email
			so that email is not required. If email is required, set 
			email.required = true; in form julian@ideasource.com.
	1.1.2: Fixed a bug where trailing . in e-mail address was passing
            (the bug is actually in the weak regexp engine of the browser; I
            simplified the regexps to make it work).
   1.1.1: Removed restriction that countries must be preceded by a domain,
            so abc@host.uk is now legal.  However, there's still the 
            restriction that an address must end in a two or three letter
            word.
     1.1: Rewrote most of the function to conform more closely to RFC 822.
     1.0: Original  */

<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->

<!-- Begin
function emailCheck (emailStr) {
// do not validate blank email
if (emailStr == "") {
	return false;
}
/* The following pattern is used to check if the entered e-mail address
   fits the user@domain format.  It also is used to separate the username
   from the domain. */
var emailPat=/^(.+)@(.+)$/
/* The following string represents the pattern for matching all special
   characters.  We don't want to allow special characters in the address. 
   These characters include ( ) < > @ , ; : \ " . [ ]    */
var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
/* The following string represents the range of characters allowed in a 
   username or domainname.  It really states which chars aren't allowed. */
var validChars="\[^\\s" + specialChars + "\]"
/* The following pattern applies if the "user" is a quoted string (in
   which case, there are no rules about which characters are allowed
   and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
   is a legal e-mail address. */
var quotedUser="(\"[^\"]*\")"
/* The following pattern applies for domains that are IP addresses,
   rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
   e-mail address. NOTE: The square brackets are required. */
var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
/* The following string represents an atom (basically a series of
   non-special characters.) */
var atom=validChars + '+'
/* The following string represents one word in the typical username.
   For example, in john.doe@somewhere.com, john and doe are words.
   Basically, a word is either an atom or quoted string. */
var word="(" + atom + "|" + quotedUser + ")"
// The following pattern describes the structure of the user
var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
/* The following pattern describes the structure of a normal symbolic
   domain, as opposed to ipDomainPat, shown above. */
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")

var email_msg = "";
/* Finally, let's start trying to figure out if the supplied address is
   valid. */

/* Begin with the coarse pattern to simply break up user@domain into
   different pieces that are easy to analyze. */
var matchArray=emailStr.match(emailPat)
if (matchArray==null) {
  /* Too many/few @'s or something; basically, this address doesn't
     even fit the general mould of a valid e-mail address. */
	email_msg = "Email address seems incorrect (check @ and .'s)";
	return email_msg;
}
var user=matchArray[1]
var domain=matchArray[2]

// See if "user" is valid 
if (user.match(userPat)==null) {
    // user is not valid
    email_msg = "The username doesn't seem to be valid.";
    return email_msg;
}

/* if the e-mail address is at an IP address (as opposed to a symbolic
   host name) make sure the IP address is valid. */
var IPArray=domain.match(ipDomainPat)
if (IPArray!=null) {
    // this is an IP address
	  for (var i=1;i<=4;i++) {
	    if (IPArray[i]>255) {
	        email_msg = "Destination IP address is invalid!";
		return email_msg;
	    }
    }
    return true
}

// Domain is symbolic name
var domainArray=domain.match(domainPat)
if (domainArray==null) {
	email_msg = "The domain name doesn't seem to be valid.";
    return email_msg;
}

/* domain name seems valid, but now make sure that it ends in a
   three-letter word (like com, edu, gov) or a two-letter word,
   representing country (uk, nl), and that there's a hostname preceding 
   the domain or country. */

/* Now we need to break up the domain to get a count of how many atoms
   it consists of. */
var atomPat=new RegExp(atom,"g")
var domArr=domain.match(atomPat)
var len=domArr.length
if (domArr[domArr.length-1].length<2 || 
    domArr[domArr.length-1].length>3) {
   // the address must end in a two letter or three letter word.
   email_msg = "The address must end in a three-letter domain, or two letter country.";
   return email_msg;
}

// Make sure there's a host name preceding the domain.
if (len<2) {
   var errStr="This address is missing a hostname!"
   email_msg = errStr;
   return email_msg;
}

// If we've gotten this far, everything's valid!
return false;
}
//  End -->




// This is the function that performs form verification. It will be invoked
// from the onSubmit() event handler. The handler should return whatever
// value this function returns.
function verify(f) {
	var errorFlag = false;
	var msg = "";
	var empty_fields = "";
	var email_field = "";
	var password_fields = "";
	var match_fields ="";
	var errors = "";
	var color_default = "01382B";
	var color_alert = "660099";
	// Loops through the elements of the form, looking for all text and textarea
	// elements that have a 'required' property defined. Then, check for fields 
	// that are empty and make a list of them. Also, if any of these elements have a 
	// 'password' property defined, then verify that it is at least 6 characters long.
	// Confirm Password Function
	// Also, if any of these elements have a "min" or a "max" property defined,
	// then verify that they are numbers and that they are in the right range. 
	// If the element has a 'numeric' property defined, verify that it is a number.
	// Put together error messages for fields that are wrong.
	for (var i=0; i < f.length; i++) {
		var e = f.elements[i];
		// If form field is a text or a textarea
		if ((e.type == "text") || (e.type == "textarea")) {
			// If field is required, check if the field is empty
			if (e.required) {
				eval(e.name).style.color = color_default;
				if ((e.value == null) || (e.value == "") || isBlank(e.value)) {
					if (!errorFlag){
						e.focus();
						errorFlag = true;
					}
					eval(e.name).style.color = color_alert;
					empty_fields += "\n         " + e.name;
					continue;
				}
			}
			// Verify Email fields
			if (e.email) {
				eval(e.name).style.color = color_default;
				email_msg = emailCheck (e.value)
				if ((e.value == null) || email_msg) {
					if (!errorFlag){
						e.focus();
						errorFlag = true;
					}
					eval(e.name).style.color = color_alert;
					email_field += email_msg;
					continue;
				}
			}
			// Check for fields that are supposed to be numeric
			if (e.numeric || (e.min != null) || (e.max != null)) {
				var v = parseFloat(e.value);
				if (isNaN(v) ||
					((e.min != null) && (v < e.min)) || 
					((e.max != null) && (v > e.max))) {
					if (!errorFlag){
						e.focus();
						errorFlag = true;
					}
					errors += " - The field " + e.name + " must be a number";
					if (e.min != null) 
						errors += " that is greater than " + e.min;
					if (e.max != null && e.min != null)
						errors += " and less than " + e.max;
					else if (e.max != null)
						errors += " that is less than " + e.max;
					errors += ".\n";
				}
			}
		}// End text/textarea validation
		
		// If form field is a password
		if ((e.type == "password")) {
			// Check for password fields
			if (e.password) {
				eval(e.name).style.color = color_default;
				// check to see if the password is at least 4 characters
				if (e.value.length < 4) {
					eval(e.name).style.color = color_alert;
					password_fields += "\n         " + e.name;
					if (!errorFlag){
						e.focus();
						errorFlag = true;
					}
					continue;
				}
			}
			// Match password if neccessary
			if (e.match) {
				eval(e.name).style.color = color_default;
				//eval(e.match.name).style.color = 'Black';
				// Check for match
				if (e.value != e.match.value) {
					eval(e.name).style.color = color_alert;
					eval(e.match.name).style.color = color_alert;
					match_fields += "\n         " + e.name +" must match " +e.match.name;
					if (!errorFlag){
						e.focus();
						errorFlag = true;
					}
				}
			}
		} // End Password validation
	}// End Loop form elements


// Now, if there were any errors, display the messages, and
// return false to prevent the form from being submitted.
// Otherwise return true.
if (!errorFlag) return true;

msg = "____________________________________________________________________________\n\n";
msg += "The form was not submitted because of the following error(s).\n";
msg += "Please correct these error(s) and re-submit.\n";
msg += "____________________________________________________________________________\n\n";

if (empty_fields)
	msg += "- The following required field(s) are empty:" + empty_fields +"\n";
if (email_field)
	msg += "- "+ email_field +"\n";
if (password_fields)
	msg += "- Your password must have at least four characters.";
if (match_fields)
	msg += "- The following fields must match:"+ match_fields +"\n";
if (errors)
	msg+= "\n" + errors;
alert(msg);
return false;
}// End Verify Function	
