Validating email using javascript

It is unit tested with 100% code coverage and can be considered fit for production.

The project can be found on Git Hub where you can also find our issue tracker.

Having a-z and A-Z means that both lowercase and uppercase letters are allowed.

indicates the minimum and maximum number of characters.

The if block in lines 3-6 checks to see if the email address in the first field passes the regex check by invoking the search() method on our email string value.

This method accepts regex expressions as arguments to check the string and returns the position of the specified value in the string or a -1 if no match was found.

On the final line we call test method for our regular expression and pass the email address as input.If not, the form submition is canceled, and the surfer prompted to re-enter a valid address.The script makes the following assumptions regarding what a valid email address is: -Contains a least one character procedding the "@" -Contains a "@" following the procedding character(s) -Contains at least one character following the "@", followed by a dot (.), followed by either a two character or three character string (a two character country code or the standard three character US code, such as com, edu etc) Note that this script requires NS 4 or IE 4 to function- all other browsers will simply not participate in the form validation process.If the email addresses passes both these tests, then the function will return a true value.The regex for validating the correct email format in line 2 has been borrowed and edited slightly by adding the i flag so that the regex is case-insensitive.

Leave a Reply