Forum Moderators: open

Message Too Old, No Replies

Repeated pattern regular expression

         

TheSeoGuy

3:19 pm on Dec 23, 2009 (gmt 0)

10+ Year Member



Not sure if this should be under the Microsoft category, but I am attempting to use a regular expression validator to check the following cradentials.

We would like to allow the user to enter either a single e-mail address into a text field, or an indefinite number of e-mail addresses seperated by commas.

I can easily check that a single e-mail address is correct with the built in regex in Visual Studio, but how can I check to ensure that the text contained within the text field matches a correct pattern and that each item is in a correct format?

For example.
test@example.com (CORRECT)
test@example.com, abc@example.com (CORRECT)
test@test, abc@example.com (NOT CORRECT)
test@example.com abc@example.com (NOT CORRECT)
test@example.com, abc@example.com, (NOT CORRECT)

Any help would be greatly appreciated.

Thanks in advance

[edited by: marcel at 4:07 pm (utc) on Dec. 23, 2009]
[edit reason] examplified [/edit]

marcel

4:05 pm on Dec 23, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm terrible at Regular Expressions, so I would probably go about it like this:

- Split the email address string at the comma
- Check each (trimmed) string against the Regular Expression
- Output a string showing all invalid email addresses
- If error string is empty, all email addresses are valid

Here is some example code, quick and dirty, so it needs some tweaking but you get the general idea.

string emails = "info@example.com, me@example.com, me@example";
string[] emailArray = emails.Split(',');
string errors = String.Empty;

foreach (string email in emailArray)
{
Regex re = new Regex(@"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
Match m = re.Match(email.Trim());
if (!m.Success)
{
errors += String.Format("{0} is not a valid Email address<br />", email.Trim());
}
}
if (!String.IsNullOrEmpty(errors))
{
Response.Write(errors);
return;
}
// else continue

TheSeoGuy

5:36 pm on Dec 23, 2009 (gmt 0)

10+ Year Member



THanks for the reply and code example marcel.

Can I do this same thing on the client side?
This particular field will not be the only field on the page with validation and I would like the validation to run all at once.

Thanks again.

marcel

6:12 pm on Dec 23, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here [regexlib.com] is a Regular expression that almost matches your requirements, might be worth a try.

marcel

9:33 am on Dec 24, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here is another idea, based on an article [imar.spaanjaars.com] by Imar Spaanjaars.

It uses Ajax to check the emails in code behind, giving a 'client side' user experience.

Code for your web page:


<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" />
<asp:TextBox ID="txtEmailAddresses" runat="server" Width="500" />
<br />
<span id="emailErrors" runat="server" style="display: none; color: Red;">* One or more emails invalid.</span>
<script type="text/javascript">
var emailTextBox = document.getElementById('<%=txtEmailAddresses.ClientID%>');
var emailErrors = document.getElementById('<%=emailErrors.ClientID%>');
function emailCheck() {
var emails = emailTextBox.value;
PageMethods.emailCheck(emails, emailErrorsCallback);
}
function emailErrorsCallback(result) {
emailErrors.style.display = result ? 'none' : 'inline';
}
$addHandler(emailTextBox, 'blur', emailCheck);
</script>
</form>

and in the code behind:


[WebMethod]
public static bool emailCheck(string emails)
{
string[] emailArray = emails.Split(',');
foreach (string email in emailArray)
{
Regex re = new Regex(@"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
Match m = re.Match(email.Trim());
if (!m.Success)
{
return false;
}
}
return true;
}

* Remember to reference the System.Web.Services namespace.

You could also adapt this to return a string, showing all of the invalid email addresses like in my first example.

TheSeoGuy

2:57 pm on Dec 29, 2009 (gmt 0)

10+ Year Member



Marcel,

Thank you for all your advice. We ended up going with the Ajax idea and parsing through each e-mail after it was split.

Thanks again