By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,813 Members | 1,236 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,813 IT Pros & Developers. It's quick & easy.

RequiredFieldValidator allows blank values when InitialValue is set!!

P: n/a
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.

--
Alan Silver
(anything added below this line is nothing to do with me)
Nov 19 '05 #1
Share this Question
Share on Google+
11 Replies


P: n/a
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.

"Alan Silver" <al*********@nospam.thanx> wrote in message
news:87**************@nospamthankyou.spam...
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.

--
Alan Silver
(anything added below this line is nothing to do with me)
Nov 19 '05 #2

P: n/a
>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.
"Alan Silver" <al*********@nospam.thanx> wrote in message
news:87**************@nospamthankyou.spam...
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.


--
Alan Silver
(anything added below this line is nothing to do with me)
Nov 19 '05 #3

P: n/a
The documentation for this class actually calls out exactly the
behavior that you are seeing.

http://msdn.microsoft.com/library/de...valuetopic.asp

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.

Nov 19 '05 #4

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

http://msdn.microsoft.com/library/de...valuetopic.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.

Nov 19 '05 #5

P: n/a
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" <al*********@nospam.thanx> wrote in message
news:er**************@nospamthankyou.spam...
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.
"Alan Silver" <al*********@nospam.thanx> wrote in message
news:87**************@nospamthankyou.spam...
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.


--
Alan Silver
(anything added below this line is nothing to do with me)
Nov 19 '05 #6

P: n/a
>Also, in response to your comment about the RegularExpressionValidator
not failing on empty text, see the documentation:

http://msdn.microsoft.com/library/de.../en-us/cpref/h
tml/frlrfsystemwebuiwebcontrolsrequiredfieldvalidatorc lassinitialvalueto
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

--
Alan Silver
(anything added below this line is nothing to do with me)
Nov 19 '05 #7

P: n/a
>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

--
Alan Silver
(anything added below this line is nothing to do with me)
Nov 19 '05 #8

P: n/a
>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/de.../en-us/cpref/h
tml/frlrfsystemwebuiwebcontrolsrequiredfieldvalidatorc lassinitialvalueto
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

--
Alan Silver
(anything added below this line is nothing to do with me)
Nov 19 '05 #9

P: n/a
So use two RequiredFieldValidators.

-Brock
DevelopMentor
http://staff.develop.com/ballen
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.

Nov 19 '05 #10

P: n/a
>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 ;-)
-Brock
DevelopMentor
http://staff.develop.com/ballen
Hello,
I am using this validator on a textbox, and have discovered that if

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.



--
Alan Silver
(anything added below this line is nothing to do with me)
Nov 19 '05 #11

P: n/a
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: PL****@PeterBlum.com
Creator of "Professional Validation And More" at
http://www.peterblum.com/vam/home.aspx

"Alan Silver" <al*********@nospam.thanx> wrote in message
news:87**************@nospamthankyou.spam...
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.

--
Alan Silver
(anything added below this line is nothing to do with me)

Nov 19 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.