Hiyasat
Hiyasat

Reputation: 8926

Regex to prevent textbox from accepting email addresses

What is a regular expression that I can use to prevent a textbox from accepting an email address?

Upvotes: 2

Views: 1319

Answers (4)

NakedBrunch
NakedBrunch

Reputation: 49413

Regex emailregex = new Regex("([0-9a-zA-Z]([-\.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})");
String s = "[email protected]";
Match m = emailregex.Match(s);
if (!m.Success) {
   //Not an email address
}

However, be very warned that others much smarter than you and I have not found the perfect regex for emails. If you want something bulletproof then stay away your current approach.

Upvotes: 4

Evgeny
Evgeny

Reputation: 11

It should be like this:

Regex emailRegex = new Regex(@"([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]    {1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})");
Match m = emailRegex.Match(this.emailAddress);
if (!m.Success) {
    MessageBox.Show("Email address is not correct");
} else { 
    Application.Exit();
}

Upvotes: 1

Nick Jones
Nick Jones

Reputation: 6493

Read How to Find or Validate an Email Address and then check for no matches or negate the expression.

Upvotes: 2

Jamiec
Jamiec

Reputation: 136124

Simply use a regex that is for an email address and check there are no matches.

Upvotes: 4

Related Questions