Regular Expression Question

G

Guest

I have a reguar expression syntax that works fine when used in a
RegularExpressionValidation control. If I use the exact same regular
expression using System.Text.RegularExpressions.Regex to validate, it doesn't
catch all invalid formats. This is for validating a single line text with
multiple email addresses. Validation conditions apart from the common
acceptable email formats are below:
1. the separator has to be either a comma or a semi-colon
2. no periods (.) just before the @ character
3. no single quotes at the end

valid text:
(e-mail address removed);[email protected];[email protected]

invlid text:
(e-mail address removed) -- NOTE THE PERIOD JUST BEFORE @
(e-mail address removed):eek:[email protected] -- NOTE THE COLON SEPARATOR
(e-mail address removed);[email protected]' -- NOTE THE SINGLE QUOTE AT THE END

Here is the sample code, this one works just fine for above conditions:

<asp:RegularExpressionValidator ID="ccValidator"
ControlToValidate="TextBox1"
ValidationExpression=
"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*?(\s*[,;]\s*\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*?)*"
Display="Dynamic"
Text="*Please enter a valid e-mail address"
runat="server" />

I want to use the same regex on the server-side as follows: This one catches
the period before @ if I have only one email address entered. If multiple
email addresses entered even with the valid separator(;), it doesn't catch
the period before @. Also doesn't catch the invalid separator - such as full
colon:)) instead of semi-colon and so on.

private void Button1_Click(object sender, System.EventArgs e) {
// I am using the TextBox1.Text here, but the text would be the single line
text data from backend that would contain multiple email addresses separated
by a , or ;
Regex emailString = new
Regex(@"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*?(\s*[,;]\s*\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*?)*");
Response.Write(emailString.IsMatch(TextBox1.Text).ToString());
}

Thanks in advance
 
G

Guest

ValidationExpression="^(\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*\s*[,;]?\b)*$"
 
G

Guest

Excellent, Thanks Phil. Works just great, but ran into one little issue.
When there is a space between the separator and the next email address, it
becomes invalid. How do I allow space(s) between the separator and email
address. Example,

(e-mail address removed); (e-mail address removed)

Thanks again in advance.


Phillip Williams said:
ValidationExpression="^(\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*\s*[,;]?\b)*$"
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com


Ramesh said:
I have a reguar expression syntax that works fine when used in a
RegularExpressionValidation control. If I use the exact same regular
expression using System.Text.RegularExpressions.Regex to validate, it doesn't
catch all invalid formats. This is for validating a single line text with
multiple email addresses. Validation conditions apart from the common
acceptable email formats are below:
1. the separator has to be either a comma or a semi-colon
2. no periods (.) just before the @ character
3. no single quotes at the end

valid text:
(e-mail address removed);[email protected];[email protected]

invlid text:
(e-mail address removed) -- NOTE THE PERIOD JUST BEFORE @
(e-mail address removed):eek:[email protected] -- NOTE THE COLON SEPARATOR
(e-mail address removed);[email protected]' -- NOTE THE SINGLE QUOTE AT THE END

Here is the sample code, this one works just fine for above conditions:

<asp:RegularExpressionValidator ID="ccValidator"
ControlToValidate="TextBox1"
ValidationExpression=
"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*?(\s*[,;]\s*\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*?)*"
Display="Dynamic"
Text="*Please enter a valid e-mail address"
runat="server" />

I want to use the same regex on the server-side as follows: This one catches
the period before @ if I have only one email address entered. If multiple
email addresses entered even with the valid separator(;), it doesn't catch
the period before @. Also doesn't catch the invalid separator - such as full
colon:)) instead of semi-colon and so on.

private void Button1_Click(object sender, System.EventArgs e) {
// I am using the TextBox1.Text here, but the text would be the single line
text data from backend that would contain multiple email addresses separated
by a , or ;
Regex emailString = new
Regex(@"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*?(\s*[,;]\s*\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*?)*");
Response.Write(emailString.IsMatch(TextBox1.Text).ToString());
}

Thanks in advance
 
Joined
Jan 7, 2009
Messages
1
Reaction score
0
In order to fix this issue you could use this regular expresion:

^(\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*\s*[,;]?\s*\b)*$

This regular expression allow spaces after and before the comma or semicolon character.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top