用于在'@'之前验证电子邮件长度的正则表达式

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?......

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

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.

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

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