Benny Sutton is 4 hire - just click here

Validate Email Addresses With JavaScript and Regex

Validate email addresses with JavaScript and Regex

Very handy code to validate input on the client in the browser using JavaScript and Regular expressions

See the working demo below.

Demo: How to validate addresses with JavaScript and Regex

JavaScript

This is pretty much the standard JavaScript you'll paste into your page script block - or in a .js file if you prefer.

            
            function isEmail(elm) {
                if (elm.value == '') {
                    alert("fill in email"); return false;
                }
                var ePat = /\b[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}\b/;
                if (ePat.test(elm.value) == false) {
                    alert(elm.value + " is NOT a valid email");
                    return false;
                }else{
                         alert(elm.value + " IS A VALID email");
                    return false; 
                }
            }
        
    

CSS

Edit as necessary.

                
            body{
              font-size:2em;
              font-weight:bold;
            }
            input{
              font-size:50px;
            }
            button{
              font-size:50px;
            }
        
    

Conclusion

A few lines of JQuery is all you need to validate email addresses with javascript and regex