-5

An open-source cms I'm working on uses the following regex for validating emails:

 valid_regex=^[_a-z0-9-]+(.[_a-z0-9-]+)@[a-z0-9-]+(.[a-z0-9-]+)(.[a-z]{2,3})$

and I need to validate also the length of the email address before the '@' symbol to accept emails with at least 2 characters.. I've read that using {2,} will do the trick but where and how exactly should I use it?......

Mithun Satheesh
  • 27,240
  • 14
  • 77
  • 101
bikey77
  • 6,384
  • 20
  • 60
  • 86
  • 1
    Read: http://haacked.com/archive/2007/08/21/i-knew-how-to-validate-an-email-address-until-i.aspx – NullUserException Dec 01 '11 at 07:26
  • 3
    Why? There are email addresses with only one character there. There are also email addresses which include a `+` character (which would be rejected by your expression) and email addresses with TLDs that have more than three characters in it (also rejected). – Quentin Dec 01 '11 at 07:26
  • It's not up to me my friend, the requirement is to accept 2 and above chars. – bikey77 Dec 01 '11 at 07:42
  • I need to change from 3 to 2 but I'm not sure how to. I changed the regex above to valid_regex=^[_a-z0-9-]+(.[_a-z0-9-]+){2,}@[a-z0-9-]+(.[a-z0-9-]+)(.[a-z]{2,3})$ but it didnt work. If you don't have an answer then simply don't bother to flame my question. Sorry if I stepped on your toes. – bikey77 Dec 01 '11 at 08:04
  • That would have been a relevant information 42 minutes ago. I'm sorry if you had to wait for an answer: one dot too much. – mario Dec 01 '11 at 08:06
  • possible duplicate of [What is the best regular expression for validating email addresses?](http://stackoverflow.com/questions/201323/what-is-the-best-regular-expression-for-validating-email-addresses) – mario Dec 01 '11 at 08:25

3 Answers3

2

to validate an email, don't use regex. instead use

if (filter_var($input, FILTER_VALIDATE_EMAIL) !== false) {

If you still want to validate length before @, a simple

if (strpos($input, '@') < 2) {

should suffice.

dqhendricks
  • 19,030
  • 11
  • 50
  • 83
  • You're aware that FILTER_VALIDATE_EMAIL is a regex? – mario Dec 01 '11 at 08:03
  • @mario actually FILTER_VALIDATE_EMAIL equates to 274 on my version of PHP. – dqhendricks Dec 01 '11 at 08:18
  • [Nevertheless it is](https://github.com/php/php-src/blob/master/ext/filter/logical_filters.c#L499). Also I'm just saying because it's just a bit too bemusing to see questions in the [regex] tag answered with "don't use regex" and then inadvertently recommend a regex. – mario Dec 01 '11 at 08:22
  • 1
    @mario I see your point. I should have stated, "don't re-invent the wheel", instead. – dqhendricks Dec 01 '11 at 08:28
  • The `=== true` might best be left out, as on success the email string will be returned. An `== true` or just leaving it to `if()` would work. – mario Dec 01 '11 at 08:34
1

Your regex will already force the email to be atleast 2 (or rather 3) characters before the @.

[_a-z0-9-]+(.[_a-z0-9-]+)
          ^            ^

In both cases you're using the + sign which symbols that the following character should be repeated 1 or many times. Note that this regex will not match several valid email-addresses and have a lot of other problems.

For a starter you should escape each dot using backslash \. and as it is now you force all addresses to have exactly one dot.

An easy solution would be to make the dot optional in your current regex:

[_a-z0-9-]+(\.?[_a-z0-9-]+)+

And I guess that you don't really want to limit the address to have only 1 dot in it. If you Do want that simply remove the last plus sign.

You can see it in action here: http://regexr.com?2vbof

Marcus
  • 12,296
  • 5
  • 48
  • 66
0

I recommend using this incredibly complicated and thoroughly tested RegEx to validate the email address:

http://fightingforalostcause.net/misc/2006/compare-email-regex.php

Mario Lurig
  • 783
  • 1
  • 6
  • 16