RequiredFieldValidator allows blank values when InitialValue is set!!

A

Alan Silver

Hello,

I am using this validator on a textbox, and have discovered that if I
set the InitialValue property, then the validator correctly fires if the
user does not change the initial value of the textbox, but does NOT fire
if the textbox is empty!!

I thought the whole point of this validator was to ensure the control
being validated had some text. Am I doing something stupid? It looks
like I am going to need *two* validators for this, one to check for the
initial value and one to check there's something there. That's stupid,
no?

Here is some code...

<asp:TextBox ID="txtAddr1" Text="address" RunAt="server" />

<br><asp:RequiredFieldValidator ID="reqTxtAddr1"
ControlToValidate="txtAddr1" InitialValue="address" ErrorMessage=""
Text="Required" Display="Dynamic" RunAt="server"/>

TIA for any light you can shed on this.
 
S

Siva M

Hi,

InitialValue specifies the text that the RequiredFieldValidator should
consider invalid for the associated control and show the error message. It
does not represent the intial value to show in the textbox.

In your case, any text other than 'address' is considered to be valid for
the textbox and hence RFV doesn't fire.

Hello,

I am using this validator on a textbox, and have discovered that if I
set the InitialValue property, then the validator correctly fires if the
user does not change the initial value of the textbox, but does NOT fire
if the textbox is empty!!

I thought the whole point of this validator was to ensure the control
being validated had some text. Am I doing something stupid? It looks
like I am going to need *two* validators for this, one to check for the
initial value and one to check there's something there. That's stupid,
no?

Here is some code...

<asp:TextBox ID="txtAddr1" Text="address" RunAt="server" />

<br><asp:RequiredFieldValidator ID="reqTxtAddr1"
ControlToValidate="txtAddr1" InitialValue="address" ErrorMessage=""
Text="Required" Display="Dynamic" RunAt="server"/>

TIA for any light you can shed on this.
 
A

Alan Silver

Hi,
InitialValue specifies the text that the RequiredFieldValidator should
consider invalid for the associated control and show the error message. It
does not represent the intial value to show in the textbox.

I know, if you look at my example, I put "address" as the initial value
in the textbox myself.
In your case, any text other than 'address' is considered to be valid for
the textbox and hence RFV doesn't fire.

Surely the words "required field" in the control name imply that the
validator should ensure the field has a non-empty value. The initial
value would simply be an extra constraint to say that the non-empty
value can't be equal to the initial value.

If I only wanted that the control shouldn't have "address" in it, then a
CompareValidator would be a more logical choice.

I'm not too impressed with these validators. They are better than
nothing, but they seem to be a long way short of the mark.

I have since discovered that if you use a RegularExpressionValidator
with a ValidationExpression of "\w{6,15}", then it also allows blank
values. Surely the very fact that I specified a minimum length should
preclude a blank value?

Any further comments welcome.
 
S

Siva M

Perhaps, you can consider having two validator controls for your textbox:
one RFV with default InitialValue (empty string) to prevent empty value and
one CompareValidator to check the value is not 'address'. Will that be ok?

Alan Silver said:
Hi,

InitialValue specifies the text that the RequiredFieldValidator should
consider invalid for the associated control and show the error message. It
does not represent the intial value to show in the textbox.

I know, if you look at my example, I put "address" as the initial value
in the textbox myself.
In your case, any text other than 'address' is considered to be valid for
the textbox and hence RFV doesn't fire.

Surely the words "required field" in the control name imply that the
validator should ensure the field has a non-empty value. The initial
value would simply be an extra constraint to say that the non-empty
value can't be equal to the initial value.

If I only wanted that the control shouldn't have "address" in it, then a
CompareValidator would be a more logical choice.

I'm not too impressed with these validators. They are better than
nothing, but they seem to be a long way short of the mark.

I have since discovered that if you use a RegularExpressionValidator
with a ValidationExpression of "\w{6,15}", then it also allows blank
values. Surely the very fact that I specified a minimum length should
preclude a blank value?

Any further comments welcome.
 
A

Alan Silver

Perhaps, you can consider having two validator controls for your
textbox: one RFV with default InitialValue (empty string) to prevent
empty value and one CompareValidator to check the value is not
'address'. Will that be ok?

I don't really have much choice do I? I am seriously considering writing
my own validators as the MS ones seem very badly designed. Most of what
they do is pretty simple anyway, it's just a shame that MS didn't do it
right.

Ta ra
 
A

Alan Silver

Also, in response to your comment about the RegularExpressionValidator
not failing on empty text, see the documentation:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/h
tml/frlrfsystemwebuiwebcontrolsrequiredfieldvalidatorclassinitialvalueto
pic.asp

It says this:

Note Validation succeeds if the input control is empty. If a value is
required for the associated input control, use a RequiredFieldValidator
control in addition to the RegularExpressionValidator control.

OK, another case of (IMO) very poor design.

If a regex has a {4,10} at the end, it surely says that the value to be
evaluated MUST be between 4 and 10 characters. To allow such a validator
to pass an empty value seems like poor design to me.

Still, I'm not in control of the design, I'm just trying to find out
what it does and how. At least I'm informed now!!

Thanks again
 
A

Alan Silver

The documentation for this class actually calls out exactly the
behavior that you are seeing.

Oh, I must have missed that. Mind you, I was using a book as a basis for
info, not the MS docs.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/h
tml/frlrfsystemwebuiwebcontrolsrequiredfieldvalidatorclassinitialvalueto
pic.asp

I must say that, even after reading that, it's not very clear that this
behaviour is intended. Yes, when you read it carefully, and with the
hindsight of knowing it does this, it does say this, but on first
reading I don't think it is easily noticeable.
This is apparently by design. Is this a good design? That is up for
discussion, but that is indeed what the documentation says that it
should do.

Oh well, I can't say I am impressed with the design choice here, but at
least it's not a bug.

Thanks for the info. Looks like I need two validators to perform this
amazingly simple task.

I think I'll write my own. I've found so many issues with the MS
validators.

Ta ra
 
A

Alan Silver

So use two RequiredFieldValidators.

I'm going to have to. I just seems poor design to need two validators
when the one should do the job.

I know, when I design ASP.NET 3.0, I'll do it differently ;-)
 
P

Peter Blum

The validators supplied with ASP.NET are not very feature rich. That's why
several third parties have created replacements to the Microsoft validators.
Mine is "Professional Validation And More"
(http://www.peterblum.com/vam/home.aspx). Its replacement for
RequiredFieldValidator allows a list of items for the InitialValue property
(actually I call it the UnassignedValues property). You can define that both
blank text and a specific text item indicates the textbox is unassigned.

--- Peter Blum
www.PeterBlum.com
Email: (e-mail address removed)
Creator of "Professional Validation And More" at
http://www.peterblum.com/vam/home.aspx
 

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,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top