Reputation:
I have an ASP.NET web form where I can can enter an email address.
I need to validate that field with acceptable email addresses ONLY in the below pattern:
[email protected]
[email protected]
[email protected]
Upvotes: 0
Views: 1835
Reputation: 65411
A regular expression to validate this would be:
^[A-Z0-9._%+-]+((@home\.co\.uk)|(@home\.com)|(@homegroup\.com))$
C# sample:
string emailAddress = "[email protected]";
string pattern = @"^[A-Z0-9._%+-]+((@home\.co\.uk)|(@home\.com)|(@homegroup\.com))$";
if (Regex.IsMatch(emailAddress, pattern, RegexOptions.IgnoreCase))
{
// email address is valid
}
VB sample:
Dim emailAddress As String = "[email protected]"
Dim pattern As String = "^[A-Z0-9._%+-]+((@home\.co\.uk)|(@home\.com)|(@homegroup\.com))$";
If Regex.IsMatch(emailAddress, pattern, RegexOptions.IgnoreCase) Then
' email address is valid
End If
Upvotes: 7
Reputation: 3011
An extension method to do this would be:
public static bool ValidEmail(this string email)
{
var emailregex = new Regex(@"[A-Za-z0-9._%-]+(@home\.co\.uk$)|(@home\.com$)|(@homegroup\.com$)");
var match = emailregex.Match(email);
return match.Success;
}
Upvotes: 0
Reputation: 11432
Patricks' answer seems pretty well worked out but has a few flaws.
A better solution in C# would be:
string emailAddress = "[email protected]";
if (Regex.IsMatch(emailAddress, @"^[A-Z0-9._%+-]+@home(?:\.co\.uk|(?:group)?\.com)$", RegexOptions.IgnoreCase))
{
// email address is valid
}
Of course to be completely sure that all email addresses pass you can use a more thorough expression:
string emailAddress = "[email protected]";
if (Regex.IsMatch(emailAddress, @"^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@home(?:\.co\.uk|(?:group)?\.com)$", RegexOptions.IgnoreCase))
{
// email address is valid
}
Upvotes: -1
Reputation: 336088
I second the use of a regex, however Patrick's regex won't work (wrong alternation). Try:
[A-Z0-9._%+-]+@home(\.co\.uk|(group)?\.com)
And don't forget to escape backslashes in a string that you use in source code, depending on the language used.
"[A-Z0-9._%+-]+@home(\\.co\\.uk|(group)?\\.com)"
Upvotes: 1
Reputation: 18747
Here's how I would do the validation using System.Net.Mail.MailAddress
:
bool valid = true;
try
{
MailAddress address = new MailAddress(email);
}
catch(FormatException)
{
valid = false;
}
if(!(email.EndsWith("@home.co.uk") ||
email.EndsWith("@home.com") ||
email.EndsWith("@homegroup.com")))
{
valid = false;
}
return valid;
MailAddress
first validates that it is a valid email address. Then the rest validates that it ends with the destinations you require. To me, this is simpler for everyone to understand than some clumsy-looking regex. It may not be as performant as a regex would be, but it doesn't sound like you're validating a bunch of them in a loop ... just one at a time on a web page
Upvotes: 6
Reputation: 10681
Use a <asp:RegularExpressionValidator ../>
with the regular expression in the ValidateExpression property.
Upvotes: 0
Reputation:
Try this:
Regex matcher = new Regex(@"([a-zA-Z0-9_\-\.]+)\@((home\.co\.uk)|(home\.com)|(homegroup\.com))");
if(matcher.IsMatch(theEmailAddressToCheck))
{
//Allow it
}
else
{
//Don't allow it
}
You'll need to add the Regex namespace to your class too:
using System.Text.RegularExpressions;
Upvotes: 0
Reputation: 4130
Here is the official regex from RFC 2822, which will match any proper email address:
(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])
Upvotes: 1
Reputation: 22065
Depending on what version of ASP.NET your are using you can use one of the Form Validation controls in your toolbox under 'Validation.' This is probably preferable to setting up your own logic after a postback. There are several types that you can drag to your form and associate with controls, and you can customize the error messages and positioning as well.
There are several types that can make it a required field or make sure its within a certain range, but you probably want the Regular Expression validator. You can use one of the expressions already shown or I think Visual Studio might supply a sample email address one.
Upvotes: 4
Reputation: 83577
You could use a regular expression.
See e.g. here:
http://tim.oreilly.com/pub/a/oreilly/windows/news/csharp_0101.html
Upvotes: 2