Reputation: 8926
What is a regular expression that I can use to prevent a textbox from accepting an email address?
Upvotes: 2
Views: 1319
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
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
Reputation: 6493
Read How to Find or Validate an Email Address and then check for no matches or negate the expression.
Upvotes: 2
Reputation: 136124
Simply use a regex that is for an email address and check there are no matches.
Upvotes: 4