Check valid email address - client/server
Lately I needed to create a webpage where an input field was needed where multiple email addresses could be entered, separated with commas. In this particular case, I only provided validation on server-side using the Regex class (namespace System.Text.RegularExpressions).
public void DoValidationForEmails(string inputString)
{
//inputString contains all email addresses, separated with commas
string separator = ",";
string[] allEmails = inputString.Split(separator.ToCharArray());
//Regular Expression for validating email addresses
Regex regularExpression = new Regex(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
foreach (string emailAddress in allEmails)
{
if (regularExpression.IsMatch(emailAddress))
{
//Valid email address
}
else
{
//Invalid email address
}
}
}
Anyone already done this on client-side as well?
Labels: ASP.NET 2.0
2 Comments:
At 8:18 PM, Anonymous said…
function DoValidationForEmails(inputString)
{
var separator = ',';
var regularExpression = new RegExp('\w+([-+.\']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*');
var allEmails = inputString.split(separator);
for(var i = 0; i < allEmails.length; ++i)
{
var emailAddress = allEmails[i];
if (regularExpression.test(emailAddress))
{
// valid e-mail address
} else
{
// invalid e-mail adress
}
}
}
At 8:24 PM, Pieter said…
Great! Good job Tim!
Post a Comment
<< Home