Email Verification JavaScript

Just a little code snippet to verify an email in JavaScript.

The function:

<script type="text/javascript">
    var result = emailVerification("insert email here");
    function emailVerification(email){
        var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
        if (!filter.test(email.value)) {
            return false;
        } else {
            return true;
        }
    }
</script>

Just call the function emailVerification with the email you want to check.

var result = emailVerification("insert email here");

Result values:

  • false means invalid email
  • true means valid email

Because simple things can make complex tasks :).

Advertisements

2 thoughts on “Email Verification JavaScript

    1. This code snippet receives in html input element and uses the value in it to test if the email is valid.

      To test a string you need to make a small change to the script, like this:

      function emailVerification(email){
              var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
              if (!filter.test(email)) {
                  return false;
              } else {
                  return true;
              }
          }

      Here’s the fiddle: http://jsfiddle.net/QAqZM/

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