473,382 Members | 1,247 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Regular Expression Question

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:
em****@company.com;my*******@otherco...2** @work.net

invlid text:
myemail.@company.com -- NOTE THE PERIOD JUST BEFORE @
my*****@company.com:ot*******@company.com -- NOTE THE COLON SEPARATOR
my*****@company.com;my******@other.com' -- 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

Apr 6 '06 #1
2 2757
ValidationExpression="^(\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*\s*[,;]?\b)*$"
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"Ramesh" wrote:
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:
em****@company.com;my*******@otherco...2** @work.net

invlid text:
myemail.@company.com -- NOTE THE PERIOD JUST BEFORE @
my*****@company.com:ot*******@company.com -- NOTE THE COLON SEPARATOR
my*****@company.com;my******@other.com' -- 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

Apr 7 '06 #2
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,

em****@company.com; em****@company2.com

Thanks again in advance.
"Phillip Williams" wrote:
ValidationExpression="^(\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*\s*[,;]?\b)*$"
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"Ramesh" wrote:
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:
em****@company.com;my*******@otherco...2** @work.net

invlid text:
myemail.@company.com -- NOTE THE PERIOD JUST BEFORE @
my*****@company.com:ot*******@company.com -- NOTE THE COLON SEPARATOR
my*****@company.com;my******@other.com' -- 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

Apr 19 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: peterbe | last post by:
I want to match a word against a string such that 'peter' is found in "peter bengtsson" or " hey peter," or but in "thepeter bengtsson" or "hey peterbe," because the word has to stand on its own....
2
by: Andrew Gaskell | last post by:
Dear all I'm having problems generating a regular expression in .NET, having just started using regular expressions. I am trying to validate a user's full name. So far I have: * as the...
3
by: Gopinath | last post by:
Hi JavaScript Gurus, I've a question on Regular Expressions using RegExp object. I just want to know whether it is possible to do the search (see below) using RegExp. Any pointers would be of...
6
by: dreamerbin | last post by:
Hi, I'm having trouble extracting substrings using regular expression. Here is my problem: Want to find the substring that is immediately before a given substring. For example: from "00...
10
by: Lee Kuhn | last post by:
I am trying the create a regular expression that will essentially match characters in the middle of a fixed-length string. The string may be any characters, but will always be the same length. In...
6
by: Ludwig | last post by:
Hi, i'm using the regular expression \b\w to find the beginning of a word, in my C# application. If the word is 'public', for example, it works. However, if the word is '<public', it does not...
12
by: stevebread | last post by:
Hi, I am having some difficulty trying to create a regular expression. Consider: <tag1 name="john"/ <br/<tag2 value="adj__tall__"/> <tag1 name="joe"/> <tag1 name="jack"/> <tag2...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.