Function to validate email in javascript

This regular expression will not verify if the particular email id exists or not. Instead, this regex is to validate the format of email id

In jquery

var ValidateEmail= function (email)
{
    var regexp = new RegExp('^[^\s@]+@[^\s@]+\.[^\s@]+$'); 
    var check = $(email).val();
    if (!regexp.test(check)) {
 
        alert('Invalid e-mail!Please enter the e-mail ID in correct format');
        $('#EmailID').css('border-color', 'red');
        return false;
    }
    else
    {
        $('#EmailID').css('border-color', 'green');
        return true;
    }
}

In HTML

@Html.TextBox("EmailID", null, new { @class = "form-control text-box", @onchange = "ValidateEmail(this)" })

Explanation

This regex is versatile as it allows the all-important international characters while not disturbing the basic something@something.something format.

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Create a free website or blog at WordPress.com.

Up ↑

Create your website at WordPress.com
Get started
%d bloggers like this: