-1

Helllo,

I'm Using this for validating emails:

JavaScript:

RegExp(/^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/);

PHP:

preg_match("/^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$/i", $email)

These could easily validate the string to be like blah@blahblah.blah, BUT I also want to allow *@blahblah.blah, How those 2 rules should be edited for this?

Thanks for your help.

behz4d
  • 1,819
  • 5
  • 35
  • 59
  • 2
    You really shouldn't invent a new regex to validate e-mails. For example your regex wouldn't allow for .info-domains. See the answers at http://stackoverflow.com/questions/201323/using-a-regular-expression-to-validate-an-email-address instead. – feeela Aug 27 '12 at 12:34
  • @sQVe The users are making filters which could control the emails they receive from, they might want to allow their company emails address, so they should be able to allow *@somedomain.com – behz4d Aug 27 '12 at 12:41
  • @feeela But I just checked, this is also OK with .info domains, I found the rule in Google, seems so many users are using it, thanks for your link by the way, I will definitely read it. – behz4d Aug 27 '12 at 12:42

1 Answers1

1

In regards to your PHP question, I would actually recommend an entirely different approach: filter_var is an excellent way to validate e-mail addresses and is part of the PHP modern framework. If you are running PHP 5.2+, that is.

Leonard
  • 3,012
  • 2
  • 31
  • 52
  • Seems it's just what I needed, it validates the usual email addresses like blah@blah.blah, and also returns true on *@blah.blah, the only thing which is left is for java script, any ideas for that RegExp? thanks – behz4d Aug 27 '12 at 13:33