473,480 Members | 3,017 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

RegEx Validation Control Bug??

I'm trying to have a form where a user has to enter a valid email address, and
I'm trying to use the RegEx Validation control to do that. The problem is that
when a user submits the form with a blank line, it is ACCEPTED even though it
does not pass the pattern match in the expression.

Another problem is that if there are leading spaces or trailing in a valid email
address, the expression fails.

Case 1:(blank) : PASSES (it shouldn't)
Case 2: jo*@sixpack.com : FAILS (leading spaces)
Case 3:jo*@sixpack.com : FAILS (trailing spaces)
<form id="Form1" method="post" runat="server">
Enter your Email address:
<asp:TextBox id="txtEmail" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator id="valRegExEmail" runat="server"
ErrorMessage="Enter a valid email address"
ValidationExpression="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"
ControlToValidate="txtEmail">*</asp:RegularExpressionValidator>
<br>
<asp:Button id="Button1" runat="server" Text="Button"></asp:Button>
<br>
<asp:ValidationSummary id="ValidationSummary1"
runat="server"></asp:ValidationSummary>
</form>

Nov 18 '05 #1
3 1942
I think in the documentation it says all validators accept empty string as
valid except the required validator.

"Mike" <do************@boo.ya> wrote in message
news:80*******************@drn.newsguy.com...
I'm trying to have a form where a user has to enter a valid email address, and I'm trying to use the RegEx Validation control to do that. The problem is that when a user submits the form with a blank line, it is ACCEPTED even though it does not pass the pattern match in the expression.

Another problem is that if there are leading spaces or trailing in a valid email address, the expression fails.

Case 1:(blank) : PASSES (it shouldn't)
Case 2: jo*@sixpack.com : FAILS (leading spaces)
Case 3:jo*@sixpack.com : FAILS (trailing spaces)
<form id="Form1" method="post" runat="server">
Enter your Email address:
<asp:TextBox id="txtEmail" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator id="valRegExEmail" runat="server"
ErrorMessage="Enter a valid email address"
ValidationExpression="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"
ControlToValidate="txtEmail">*</asp:RegularExpressionValidator>
<br>
<asp:Button id="Button1" runat="server" Text="Button"></asp:Button>
<br>
<asp:ValidationSummary id="ValidationSummary1"
runat="server"></asp:ValidationSummary>
</form>

Nov 18 '05 #2
You need to use a required field validator in conjunction with a regular
expression validator. I'm guessing Microsoft have done it this way for
scenarios where fields only need to be validated if data is entered, though
it would have been nice if they could have added an option into the other
validators where you can force a field to be required.

As for the spaces problem... You could modify the regular expression to
allow leading or trailing spaces, and then trim the whitespace from the
string on the server side. Another way to do it, would be to write a
javascript function to trim the spaces client side, and update the textbox.

I.e.

<script language="javascript">
<!--
function trimWhitespace(e)
{
e.value = e.value.trim();
}
</script>

Add this to the textbox, in the codebehind file:

txtEmail.Attributes.Add("onChange", "trimWhitespace(this)")
Hope this helps,

Mun

"Mike" <do************@boo.ya> wrote in message
news:80*******************@drn.newsguy.com...
I'm trying to have a form where a user has to enter a valid email address, and I'm trying to use the RegEx Validation control to do that. The problem is that when a user submits the form with a blank line, it is ACCEPTED even though it does not pass the pattern match in the expression.

Another problem is that if there are leading spaces or trailing in a valid email address, the expression fails.

Case 1:(blank) : PASSES (it shouldn't)
Case 2: jo*@sixpack.com : FAILS (leading spaces)
Case 3:jo*@sixpack.com : FAILS (trailing spaces)
<form id="Form1" method="post" runat="server">
Enter your Email address:
<asp:TextBox id="txtEmail" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator id="valRegExEmail" runat="server"
ErrorMessage="Enter a valid email address"
ValidationExpression="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"
ControlToValidate="txtEmail">*</asp:RegularExpressionValidator>
<br>
<asp:Button id="Button1" runat="server" Text="Button"></asp:Button>
<br>
<asp:ValidationSummary id="ValidationSummary1"
runat="server"></asp:ValidationSummary>
</form>


Nov 18 '05 #3
You need to run a Required Field validation control first, then the regular
expression like this:

<form id="Form1" method="post" runat="server">
Enter your Email address:
<asp:TextBox id="email" runat="server" />
<asp:RequiredFieldValidator
id="RequiredEmail"
runat="server"
ControlToValidate="email"
ErrorMessage="<br>* Email address is required"
Display="Dynamic"
/>
<asp:RegularExpressionValidator
id="RegExpEmail"
runat="server"
ControlToValidate="email"

ValidationExpression="^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z][\w\.-]*[a-zA-Z0-
9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$"
ErrorMessage="<br>* Email is not in a valid format"
Display="Dynamic"
/>
<br>
<asp:Button id="Button1" runat="server" Text="Button"></asp:Button>
<br>
</form>

"Mike" <do************@boo.ya> wrote in message
news:80*******************@drn.newsguy.com...
I'm trying to have a form where a user has to enter a valid email address, and I'm trying to use the RegEx Validation control to do that. The problem is that when a user submits the form with a blank line, it is ACCEPTED even though it does not pass the pattern match in the expression.

Another problem is that if there are leading spaces or trailing in a valid email address, the expression fails.

Case 1:(blank) : PASSES (it shouldn't)
Case 2: jo*@sixpack.com : FAILS (leading spaces)
Case 3:jo*@sixpack.com : FAILS (trailing spaces)
<form id="Form1" method="post" runat="server">
Enter your Email address:
<asp:TextBox id="txtEmail" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator id="valRegExEmail" runat="server"
ErrorMessage="Enter a valid email address"
ValidationExpression="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"
ControlToValidate="txtEmail">*</asp:RegularExpressionValidator>
<br>
<asp:Button id="Button1" runat="server" Text="Button"></asp:Button>
<br>
<asp:ValidationSummary id="ValidationSummary1"
runat="server"></asp:ValidationSummary>
</form>

Nov 18 '05 #4

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

Similar topics

3
2367
by: Alan Pretre | last post by:
Can anyone help me figure out a regex pattern for the following input example: xxx:a=b,c=d,yyy:e=f,zzz:www:g=h,i=j,l=m I would want four matches from this: 1. xxx a=b,c=d 2. yyy e=f 3....
1
1688
by: Colin Reid | last post by:
Hey MS, here's an apparent problem in the base class library. I pulled the email validation pattern "^((*)*@(()+(*)*\.)+{2,9})" from http://regexlib.com. If I validate the email address...
5
318
by: Shaun Wilde | last post by:
When using the regular expression validator is there a way of failing a validation when you detect a match - I suppose a sort of anti-match. I want to detect certain things on user input and if...
2
1564
by: Hailstorm | last post by:
How can I control if "a textbox is empty or not" with regex validation? I don't want to use required field validator because I have a masked textbox control and it has "ValidationExpress" property....
4
1350
by: | last post by:
Here is an interesting one. Running asp.net 2.0 beta 2. I have a regular expression used in a regex validator that works on the client side in Firefox but not in IE. Any ideas? IE always reports...
3
3956
by: jab3 | last post by:
Hello. I"m new to this group, and to JavaScript in general, so please forgive me if I breach local etiquette. I'm trying to implement some client-side 'dynamic' validation on a form. I'm having...
1
1929
by: Jim Dornbush | last post by:
Has anyone seen an updated regex expression from Microsoft for the email validation expression so that single quotes are allowed? I've been using the canned regex for emails, but recently been...
11
1131
by: shapper | last post by:
Hello, I need to create a REGEX which accepts only phone numbers. The phone numbers start allways with 261, 21, 96 or 91 and have 7 numbers after it. Something like. 261 1223346, 21...
10
1845
by: bullockbefriending bard | last post by:
first, regex part: I am new to regexes and have come up with the following expression: ((1|),(1|)/){5}(1|),(1|) to exactly match strings which look like this: 1,2/3,4/5,6/7,8/9,10/11,12 ...
0
7055
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7060
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7106
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7022
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4501
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3013
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1311
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
572
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
206
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.