RegularExpressionValidator problem

G

Guest

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.
 
G

Guest

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.
 
G

Guest

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 said:
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 said:
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.
 
R

Riki

curious said:
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.
 
G

Guest

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 said:
curious said:
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.
 

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

No members online now.

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,783
Latest member
RickeyDort

Latest Threads

Top