shiva
shiva

Reputation: 429

Email validatior Javascript

Here is my javascript code

 $scope.isValidEmailAddress = function(emailAddress) {
            // var pattern = new RegExp(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/);
            var pattern = new RegExp(/^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/);
            return pattern.test(emailAddress);
        };

When i validate below email it will return true but this is not, I need to validate this type of email

[email protected]

It should be invalid email

Upvotes: 0

Views: 125

Answers (2)

David
David

Reputation: 1144

Get the regexp from email method of jquery validate plugin:

https://cdn.jsdelivr.net/jquery.validation/1.16.0/jquery.validate.js

Upvotes: 0

Mike
Mike

Reputation: 3950

Updated as per comment.

If you want to make it possible to add up to two top level domain names you can make the second domain optional.

$scope.isValidEmailAddress = function(emailAddress) {
    // Same regex as before but an additional TLD regex has been added.
    var pattern = new RegExp(/^[\w\-\.\+]+\@[a-zA-Z0-9\-]+\.([a-zA-z0-9]{2,4}\.)?[a-zA-z0-9]{2,4}$/);
    return pattern.test(emailAddress);
};

Upvotes: 1

Related Questions