Simple RegEx Validation using jQuery

How to validate an email filed using RegEx 

HTML

Name <input type=”text” id=”txtEmail” />
<input type=”button” id=”btnOk” value=”OK”/>

jQuery

$(document).ready(function () {
       $(‘#btnOk’).live(‘click’, function () {
       email_address = $(‘#txtEmail’);
       email_regex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/i;
       if (!email_regex.test(email_address.val())) {
            alert(‘this is not valid email’);
            e.preventDefault(); return false;
         }
           else {
            alert(‘Valid email’);
           }

      });
});

                                                                                        Author : Aneesh Sivan
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s