473,407 Members | 2,598 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,407 software developers and data experts.

RegularExpressionValidator problem

I am using a RegularExpressionValidator to validate a TextBox. I use
"^[\+|-]?[0-9]+(\.[0-9]*)?$" to check for a real number. The control works
fine as long as the user enters something in the TextBox; it does not work if
the user leaves the TextBox blank. Is there a bug in the implementation of
the RegularExpressionValidator? The above regluar expression should also
catch no entry by the user.

I have added a RequiredFieldValidator to solve the problem, but it does not
make sense to have two vlidators for the same textbox: a
RegularExpressionValidator and a RequiredFieldValidator.

Thanks.

--
ASP.NET fan.
Dec 24 '06 #1
4 6029
Hi,
If you are using C# 2.0(Visual Studio 2005) and you want to do your
described validation by using single validator control then you can go for
CustomValidator Control because in ASP.NET 2.0 you can set
CustomValidator.ValidateEmptyText property to true.By this Custom Validation
will be performed on empty values also.
If you are using C# 1.0 then you can try following code/logic:

public void Validate() {

this.IsValid = true;

bool IsDataPresent = (this.TextBox1.Trim()==string.Empty );

//check here if control requires entry and does not have one

if (IsEntryRequired && IsDataPresent ) {

this.IsValid = false;

}

if (IsValid && !IsDatapresent && (this.RegExpression != null ||
this.RegExpression != "")) {

Regex validCheck = new Regex(RegExpression);

this.IsValid = validCheck.IsMatch(this.TextBox1.Trim());

}

This Validate Function i have found from the internet.
Hope this helps you out.

Thanks and Regards,
Manish Bafna.
MCP and MCTS.

"curious" wrote:
I am using a RegularExpressionValidator to validate a TextBox. I use
"^[\+|-]?[0-9]+(\.[0-9]*)?$" to check for a real number. The control works
fine as long as the user enters something in the TextBox; it does not work if
the user leaves the TextBox blank. Is there a bug in the implementation of
the RegularExpressionValidator? The above regluar expression should also
catch no entry by the user.

I have added a RequiredFieldValidator to solve the problem, but it does not
make sense to have two vlidators for the same textbox: a
RegularExpressionValidator and a RequiredFieldValidator.

Thanks.

--
ASP.NET fan.
Dec 24 '06 #2
Thanks for the response. Your response tells me, indirectly, that
RegularExpressionValidator control has a bug and does not work properly. The
regular expression that I used ( "^[\+|-]?[0-9]+(\.[0-9]*)?$") works
perfectly on the server side but not on the client side when used in
RegularExpressionValidator control. There are a number of solutions to my
problem, including the ones you sent me. I just wanted to know if I did not
use the control properly or the control is faulty. Based on your answer it is
the control's problem and we have to use another control or work around it.

--
ASP.NET fan.
"Manish Bafna" wrote:
Hi,
If you are using C# 2.0(Visual Studio 2005) and you want to do your
described validation by using single validator control then you can go for
CustomValidator Control because in ASP.NET 2.0 you can set
CustomValidator.ValidateEmptyText property to true.By this Custom Validation
will be performed on empty values also.
If you are using C# 1.0 then you can try following code/logic:

public void Validate() {

this.IsValid = true;

bool IsDataPresent = (this.TextBox1.Trim()==string.Empty );

//check here if control requires entry and does not have one

if (IsEntryRequired && IsDataPresent ) {

this.IsValid = false;

}

if (IsValid && !IsDatapresent && (this.RegExpression != null ||
this.RegExpression != "")) {

Regex validCheck = new Regex(RegExpression);

this.IsValid = validCheck.IsMatch(this.TextBox1.Trim());

}

This Validate Function i have found from the internet.
Hope this helps you out.

Thanks and Regards,
Manish Bafna.
MCP and MCTS.

"curious" wrote:
I am using a RegularExpressionValidator to validate a TextBox. I use
"^[\+|-]?[0-9]+(\.[0-9]*)?$" to check for a real number. The control works
fine as long as the user enters something in the TextBox; it does not work if
the user leaves the TextBox blank. Is there a bug in the implementation of
the RegularExpressionValidator? The above regluar expression should also
catch no entry by the user.

I have added a RequiredFieldValidator to solve the problem, but it does not
make sense to have two vlidators for the same textbox: a
RegularExpressionValidator and a RequiredFieldValidator.

Thanks.

--
ASP.NET fan.
Dec 25 '06 #3
curious wrote:
I am using a RegularExpressionValidator to validate a TextBox. I use
"^[\+|-]?[0-9]+(\.[0-9]*)?$" to check for a real number. The control
works fine as long as the user enters something in the TextBox; it
does not work if the user leaves the TextBox blank. Is there a bug in
the implementation of the RegularExpressionValidator? The above
regluar expression should also catch no entry by the user.

I have added a RequiredFieldValidator to solve the problem, but it
does not make sense to have two vlidators for the same textbox: a
RegularExpressionValidator and a RequiredFieldValidator.
It does make sense, because that is the way it was designed.

Only the RequiredFieldValidator checks for empty fields, so most
of the time you'll need 2 validators. The same for RangeValidators and
CompareValidators.

--

Riki

Dec 25 '06 #4
The reason I stated that it does not make sense to have two controls validate
a textbox is because Microsoft calls its control RegularExpressionValidator.
If it uses regular Expressions to validate a control, it must respond to a
regular expression the standard way, the way any language such as c#, pearl,
etc. respond. Microsoft should not define a new version of regular
expressions for its controls. I have used regular expressions for many years,
and when I use it in an ASP.NET conrol called RegularExpressionValidator I
expect it to obey the standard rules of regular expressions. It bothers me (
and a lot of other people) when my code does not work as expected and after a
lot of testing I learn that Microsoft has redefined the standard regular
expressions in a control called RegularExpressionValidator.

Regards.
--
ASP.NET fan.
"Riki" wrote:
curious wrote:
I am using a RegularExpressionValidator to validate a TextBox. I use
"^[\+|-]?[0-9]+(\.[0-9]*)?$" to check for a real number. The control
works fine as long as the user enters something in the TextBox; it
does not work if the user leaves the TextBox blank. Is there a bug in
the implementation of the RegularExpressionValidator? The above
regluar expression should also catch no entry by the user.

I have added a RequiredFieldValidator to solve the problem, but it
does not make sense to have two vlidators for the same textbox: a
RegularExpressionValidator and a RequiredFieldValidator.

It does make sense, because that is the way it was designed.

Only the RequiredFieldValidator checks for empty fields, so most
of the time you'll need 2 validators. The same for RangeValidators and
CompareValidators.

--

Riki

Dec 25 '06 #5

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

Similar topics

0
by: Andy Eshtry | last post by:
I have a radio button list, a textbox representing SIN or EIN based on my radio button list selection so I put 2 regularexpressionvalidator to evaluate the value of textbox. EIN must be (for...
3
by: Ricardo Corsi P. Cesar | last post by:
Hi, i looking for example in asp.net (VB) to make the RegularExpressionValidator in runtime in my code behind. I found some similars codes, but nothing in VB.. thanks!
1
by: Ken Grimmett | last post by:
I have added a REV to an input type of file. I want to make sure that the only file type that is allowed is a .gif, .jpg, or .jpeg. I develop my site on my laptop which is running Win XP Pro and...
2
by: Enzo Marinelli | last post by:
Hello everyone I have a MultiLine TextBox, as follows <asp:TextBo Id="StreetAddress Rows="4 Runat="Server TextMode="MultiLine Width="100%"/
3
by: tshad | last post by:
I have a RegularExpressionValidator that doesn't seem to work correctly if you don't enter anything. In the following, it works correctly if you have at least 1 character. If you just enter...
2
by: Sparky Arbuckle | last post by:
I've got a RegularExpressionValidator that partly works. Let me explain: With the Validator below I am only able to search for one letter/number at a time. If I want to search for Diana I see...
1
by: franz | last post by:
does anybody tell me why it doesn't works?? in any case gg beame true thanks franz Dim y As RegularExpressionValidator = New RegularExpressionValidator y.ValidationExpression =...
1
by: John Yopp | last post by:
I'm trying to use a asp:RegularExpressionValidator to validate the strength of a password. When I test the regular expression with Regex.IsMatch, it works perfectly. However, when used within a...
9
by: Nathan Sokalski | last post by:
I have several TextBoxes with TextMode="MultiLine" in which I want to limit the number of characters that can be entered using a RegularExpressionValidator. I have come up with the following...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.