0

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

rohan@yahoo.co.in.in

It should be invalid email

shiva
  • 429
  • 1
  • 6
  • 18
  • Please explain what it is which makes the address invalid. I get the impression that you mean the doubled top-level domain, but I am not sure. Providing more sample input with desired result might be helpful. – Yunnosch Jun 20 '17 at 05:34
  • [Validate email address in JavaScript?](https://stackoverflow.com/questions/46155/validate-email-address-in-javascript) – FortyTwo Jun 20 '17 at 05:38

2 Answers2

1

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);
};
Mike
  • 3,830
  • 2
  • 16
  • 23
0

Get the regexp from email method of jquery validate plugin:

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

David
  • 1,116
  • 3
  • 18
  • 32