0

i am not very experienced in Regular Expression so its why i am asking you :) my question is i use this pattern when i validate Emails.

/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zAZ\-0-9]+\.)+[a-zA-Z]{2,}))$/

what is it to add to this pattern to disallow Arabic characters ?

Blachshma
  • 17,097
  • 4
  • 58
  • 72
Marwan
  • 2,362
  • 1
  • 20
  • 35

3 Answers3

6

Regular expressions should not be used to validate emails.

The correct way to validate an email address is using the MailAddress class like this:

try 
{
    string address = new MailAddress(address).Address;
} 
catch(FormatException) 
{
    //address is invalid
}

Regarding the question itself, after you see that it is a valid email address - you can check for arabic characters.

Community
  • 1
  • 1
Blachshma
  • 17,097
  • 4
  • 58
  • 72
  • 1
    This will only work in C#, but not in JavaScript. See the question tags. – cimnine Nov 12 '12 at 08:48
  • its a very good post you sent me i used this /^([\w\!\#$\%\&\'\*\+\-\/\=\?\^\`{\|\}\~]+\.)*[\w\!\#$\%\&\'\*\+\-\/\=\?\^\`{\|\}\~]+@((((([a-z0-9]{1}[a-z0-9\-]{0,62}[a-z0-9]{1})|[a-z])\.)+[a-z]{2,6})|(\d{1,3}\.){3}\d{1,3}(\:\d{1,5})?)$/i – Marwan Nov 12 '12 at 08:49
  • @cimnine He will need to perform Server Side validation either way, and any check he will do in Javascript using RegEx will not be enough. – Blachshma Nov 12 '12 at 08:49
  • @Blachshma One for you. Yet you still did not tell how to check for arabian characters, which was the problem in the first place ;-) – cimnine Nov 12 '12 at 08:52
  • 1
    @Blachshma Oh, sorry. My bad. – cimnine Nov 12 '12 at 08:54
0

I bet you could do it with a bracket expression (aka Character Set aka Character Class) and unicode escapes (available in javascript and C#):

[^\u####-\u%%%%]

... where the hashtags (####) represent the first arabian character (i.e. the character with the lowest unicode value), and the percent signs (%%%%) the last arabian character (i.e. the character with the highest unicode value).

Wikipedia tells me that there are multiple ranges of arabian characters, so you'd need to repeat the snippet above.

cimnine
  • 3,987
  • 4
  • 33
  • 49
0

Use Character Properties:

/\p{sc=Arabic}/

matches all Arabic characters.

Then inverse the chracters that the expression matches to

/[^\p{sc=Arabic}]/
Andreas Neumann
  • 10,734
  • 1
  • 32
  • 52