-1

I want to extend the standard asp.net email regex validator to exclude a specific email.

This is the original:

 ValidationExpression="(\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*)" 

This is the extended that I produced:

 ValidationExpression="(\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*)|(?!^email\@email\.com$)" 

Can't figure out why this doesn't work

P.s. I know that the standard asp.net regex validator is not recommended for email validation, but it is OK for me.

Paolo Stefan
  • 10,112
  • 5
  • 45
  • 64
Registered User
  • 3,669
  • 11
  • 41
  • 65
  • I don't know ASP, but your snippet seems to miss a closing `)` at the end before the `"` – Gilles Quénot May 13 '12 at 21:11
  • I don't know ASP, but regex is not a tool for matching **specific** strings. Can't you create a black list of some sort? P.S. Isn't that an OR between the initial pattern and the address? – Lev Levitsky May 13 '12 at 21:14

2 Answers2

1

It does not work as the first part of your pattern matches so it doesn't use your negated second part. You are basically checking if pattern1 or pattern2 matches.

Have a look at https://stackoverflow.com/a/271946/832273 how to create your own validator. This allows you to do all kind of different checks in your program yourself without the need to put everything into your regex.

Community
  • 1
  • 1
Ulrich Dangel
  • 4,515
  • 3
  • 22
  • 30
1
^(?!^email@email\.com$)[originalregexhere]

This is doing a negative lookahead right at the beginning of the matching.

The full example:

ValidationExpression="(?!^email@email\.com$)(\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*)" 
Registered User
  • 3,669
  • 11
  • 41
  • 65
usr
  • 168,620
  • 35
  • 240
  • 369