JS form Email List Validation

Using the JQuery .each() function you can capture if a list of inputs are a proper format.  For this example, we’re checking EACH input with the class value set to requiresEmail.

$('.requiresEmail').each()

In the Each statement, you’ll call a function to perform the actual validation.  In this example, I am calling the isValidEmail function and testing the input’s value against the trust regex email validation.

	var OverRide = false;

	function setOverRide(){
		OverRide = true;
	}
	function isValidEmail(Value){
                var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,4}$/;
		return 	emailPattern.test(Value);
	}
	function CheckEmails(){
		if (OverRide)
			return true;

		returnVal = true;
		$('.requiresEmail').each(	
                   function () {
			if (!isValidEmail($(this).val()))
			{
				$(this).css('background-color', '#f1c7c4');
				returnVal = false;
			}
			else
				$(this).css('background-color', '#fff');
		   }
                );

		return returnVal;	
	}

The CheckEmails function should be called to execute the script, and there is a flag to override the validation.

Leave a Reply

Your email address will not be published. Required fields are marked *