0

Here is the original regex

/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/

This will validate email properly but if I type @test.com it was also allowed. I added {1}

/^([A-Za-z0-9_\-\.]{1})+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/

I tested this in gskinner.com and working fine. It will not allow @test.com.

But in my site, it still not working. it will still allow @test.com

Jorge
  • 5,610
  • 18
  • 47
  • 67
  • See here: http://stackoverflow.com/questions/201323/using-a-regular-expression-to-validate-an-email-address – Michael W Apr 21 '13 at 16:18
  • 1
    Why do you put your `+` outside of the capture group? This seems like you'll have problems actually capturing the pieces. – beatgammit Apr 21 '13 at 16:18
  • 1
    from: http://net.tutsplus.com/tutorials/other/8-regular-expressions-you-should-know/ `/^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/` – charly Apr 21 '13 at 16:19
  • *"It will not allow @test.com...But in my site, it still not working. it will still allow @test.com."* Well, it's going to be impossible to help you without seeing how you're (mis)applying the pattern on your site, since you say the pattern works when not used on your site. – T.J. Crowder Apr 21 '13 at 16:20
  • 3
    This has been asked and answered before. See [this answer](http://stackoverflow.com/a/201378/157247) for why you can't do this with a JavaScript regular expression, and [this answer](http://stackoverflow.com/a/46181/157247) for a version a lot of people would probably use that works in the simple case but fails in some valid cases (for the reasons in the first link). – T.J. Crowder Apr 21 '13 at 16:22
  • It's a shame that the possible duplicate's accepted answer is lousy. The only right answer is not to use a regexp, per Stephane Bortzmeyer's answer linked by TJCrowder. They're inadequate for the purpose. – Alnitak Apr 21 '13 at 16:34
  • I got the answer now, thanks. It was because of my `+`. I put it inside the parenthesis. – Jorge Apr 21 '13 at 16:38

2 Answers2

1

So, it looks like you've got some issues with your regex.

/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/

The plus after the first group should be within the parentheses and that plus is actually what you want to be able to reject the case of "@test.com" In regex the + means that the pattern must match one or more characters, but since it's not in your capturing group ([A-Za-z0-9_-.]) it's not reflecting that.

Your proposed "fix" with the addition of the {1} implies that your first group should only match subgroups with a length of one, and as such it will error if you ever try to re-use this pattern in slightly different cases.

Move the plus within the parens in your first bit of code and you should be fine.

Slater Victoroff
  • 21,376
  • 21
  • 85
  • 144
  • @Jorge. This answer is incorrect. Neither of the OP's regex will match `@test.com`, and `([A-Za-z0-9_\-\.])+` and `([A-Za-z0-9_\-\.]+)` are equivalent in terms of what they match (but not in what is captured). – MikeM Apr 21 '13 at 17:38
0

Google's regex for validating emails will cover 99% of use cases:

/**
 * Checks if the provided string is a valid address spec (local@domain.com).
 * @param {string} str The email address to check.
 * @return {boolean} Whether the provided string is a valid address spec.
 */
goog.format.EmailAddress.isValidAddrSpec = function(str) {
  // This is a fairly naive implementation, but it covers 99% of use cases.
  // For more details, see http://en.wikipedia.org/wiki/Email_address#Syntax
  // TODO(mariakhomenko): we should also be handling i18n domain names as per
  // http://en.wikipedia.org/wiki/Internationalized_domain_name
  var filter =
      /^[+a-zA-Z0-9_.!#$%&'*\/=?^`{|}~-]+@([a-zA-Z0-9-]+\.)+[a-zA-Z0-9]{2,6}$/;
  return filter.test(str);
};

From goog.format.EmailAddress class of Google's closure library.

eyecatchUp
  • 10,032
  • 4
  • 55
  • 65