-1

what I need is not email validation.. Its simple. Allow @hello.world or @hello_world or @helloworld but @helloworld. should be taken as @helloworld so as @helloworld?

In short check for alphabet or number after . and _ if not than take the string before it.

My existing RegEx is /@.([A-Za-z0-9_]+)(?=\?|\,|\;|\s|\Z)/ it only cares with @helloworld and not the @hello.world or @hello_world.

Update:

So now I got a regex which deals with problem number 1. i.e. Allow @hello.world or @hello_world or @helloworld but still What about @helloworld. should be taken as @helloworld so as @helloworld?

New RegEx: /@([A-Za-z0-9+_.-]+)/

Borderless.Nomad
  • 761
  • 1
  • 10
  • 23

4 Answers4

6

Don't use a regex for that.

Use...

$valid = filter_var($str, FILTER_VALIDATE_EMAIL);
alex
  • 479,566
  • 201
  • 878
  • 984
1

Regex will never be able to verify an email, only to do some very basic format checking.

The most comprehensive regex for matching email addresses was 8000 chars long, and that one is already invalid due to changes in what is accepted in emails.

Use some designed library for the checking if you need to get real verification, otherwise just check for @ and some dots, anything more and you will probably end up invalidating perfectly legal email addresses.

Some examples of perfectly legal email addresses: (leading and trailing " are for showing boundary only"

"dama@nodomain.se"
"\"dama\"@nodomain.se"
"da/ma@nodomain.se"
"dama@nõdomain.se"
"da.ma@nodomain.se"
"dama@pa??de??µa.d???µ?"
"dama @nodomain .se"
"dama@nodomain.se "
David Mårtensson
  • 7,550
  • 4
  • 31
  • 47
0

You can use this regexp to validate email addresses

^[A-Z0-9._%+-]+@[A-Z0-9.-]+.[A-Z]{2,6}$.

For more information and complete complete expressions you can check here

I hope this helps you

Nuno Costa
  • 1,620
  • 17
  • 14
  • Be aware that filter_var has some issues validating email, some users report that it validates emails without a TLD, I'm pretty sure that is not what you want – Nuno Costa Oct 06 '11 at 11:06
  • This will note acctept "David Martensson"@test.org which is a valid email address, also David/* djhfkjshfksdh */djfhd@test.org is a valid email with an embedded comment ;) – David Mårtensson Oct 06 '11 at 11:07
  • @NunoCosta: It is valid to not have an extension in an email. Or have a port. Or many other things this regex won't match. – alex Oct 06 '11 at 11:08
0

Try this:

\@.+(\.|\?|;|[\r\n\s]+)
Nikola K.
  • 7,093
  • 13
  • 31
  • 39
KodeFor.Me
  • 13,069
  • 27
  • 98
  • 166
  • what I need is not email validation.. Its simple. Allow '@hello.world' or '@hello_world' or '@helloworld' but '@helloworld.' should be taken as '@helloworld' so as '@helloworld?' – Borderless.Nomad Oct 07 '11 at 07:14