How to validate Gmail ids in a text box using ASP.NET RegularExpressionValidator

StackOverflow https://stackoverflow.com/questions/23652409

  •  22-07-2023
  •  | 
  •  

Вопрос

Problem:-I want to validate an email id text box..I want to block Gmail id users

Eg:-

  • abcd@yahoo.com =>Ok
  • abcd@xmail.com =>Ok
  • abcd@hotmail.com =>Ok
  • abcd@gmail.com =>Not Ok

If any user entering Gmail id in the text box..the error msg will appear..??

already tried using

\w+([-+.']\w+)*@(?:gmail).com

But it Used for selecting only Gmail ids..I want negation of this..

Это было полезно?

Решение

Change (?: ) to (?! ) to match if content of ( ) is not present:

\w+([-+.']\w+)*@(?!gmail).com

This will not match abcd@gmail.com, but will fail also on abcd@yahoo.com because no other characters allowed between @ and .com

To keep gmail.com blocked and to allow other domains change pattern to

\w+([-+.']\w+)*@((?!gmail\.com).*)

Note that when you have [-+.'] you tell to allow -,+,. and ' in the email address. Minus and dots are allowed but not plus and apostrophe.

So, finally

\w+([-.]\w+)*@((?!gmail\.com).*)

Другие советы

I think you can use negation of that

\w+([-+.']\w+)*@(?:gmail).com

In an if block check the email using this and take the negation..I think it will work

Try below regular expression:

\b[\w\.-]+@((?!gmail).)[\w\.-]+\.\w{2,4}\b
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top