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)[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
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)[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