468,106 Members | 1,474 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,106 developers. It's quick & easy.

Help with Custom Validation

Hi,

I'm relatively new to Web Forms, I have been using Required Field
Validators and Regular Expression Validators on a Web Form I am
developing and everything works as expected using Visual Studio.NET
2003 and VB code behind.

I have a radio button and textbox, and basically i need the textbox to
be required based on the user selecting "Yes" from the radio button.
I've read through the newsgroups and found that a Custom Validator
could achieve this result, but am unsure how to implement it.

I have added a Custom Validator then a Required Field Validator to be
fired should the radiobutton be "Yes" and included it in the codebehind
page in the Custom Validator as below:

Private Sub CustomValidator1_ServerValidate(ByVal source As
System.Object, ByVal args As
System.Web.UI.WebControls.ServerValidateEventArgs) Handles
CustomValidator1.ServerValidate

if radiobutton.selectedvalue.text = "Yes"

then requiredfield.isvalid = true
else
requiredfield.isvalid = false

End if

End Sub
When I run the above the required field validator runs whatever is
checked by the radio button, so I'm obviously missing something or am I
thinking too logically???. I assumed that the above code would make the
required field validator "true" if "Yes" was selected and then display
the "error" and the validator would not fire based on "no" being
selected.

Any help would be grateful...I have a suspicion I'm going in the wrong
direction!!.

Cheers
Mattyw

Nov 19 '05 #1
5 2529
I think maybe what you mean to have is this:

if radiobutton.selectedvalue.text = "Yes"

then requiredfield.enabled = true
else
requiredfield.enabled = false

End if

You want the validator "turned-on" or "turned-off" not necessarily
validated or not validated. It's still pre-coffee for me this morning so
let me know if I am off the mark here.

Clint Hill
H3O Software
http://www.h3osoftware.com

Mattyw wrote:
Hi,

I'm relatively new to Web Forms, I have been using Required Field
Validators and Regular Expression Validators on a Web Form I am
developing and everything works as expected using Visual Studio.NET
2003 and VB code behind.

I have a radio button and textbox, and basically i need the textbox to
be required based on the user selecting "Yes" from the radio button.
I've read through the newsgroups and found that a Custom Validator
could achieve this result, but am unsure how to implement it.

I have added a Custom Validator then a Required Field Validator to be
fired should the radiobutton be "Yes" and included it in the codebehind
page in the Custom Validator as below:

Private Sub CustomValidator1_ServerValidate(ByVal source As
System.Object, ByVal args As
System.Web.UI.WebControls.ServerValidateEventArgs) Handles
CustomValidator1.ServerValidate

if radiobutton.selectedvalue.text = "Yes"

then requiredfield.isvalid = true
else
requiredfield.isvalid = false

End if

End Sub
When I run the above the required field validator runs whatever is
checked by the radio button, so I'm obviously missing something or am I
thinking too logically???. I assumed that the above code would make the
required field validator "true" if "Yes" was selected and then display
the "error" and the validator would not fire based on "no" being
selected.

Any help would be grateful...I have a suspicion I'm going in the wrong
direction!!.

Cheers
Mattyw

Nov 19 '05 #2
Clint, Hi

Thanks for your response, what you said is exactly what I want...I only
want the validation to work if "Yes" is selected, if "No" is selected
then the textbox is not required.

I've changed the code to how you described and it works in so much that
it asks for validation if you select "Yes" and if you enter details it
validates correctly, however it does the same thing you answer "No"

This is the only custom validation on the page, all other validation
appears to work fine but obviously I just configured that with the GUI!

Nov 19 '05 #3
The logic is wrong.

1. If the RadioButton is not checked, the validator should report no error.
It should return IsValid=true.
2. If the RadioButton is checked, test the value of the TextBox for text.
3. Do not set IsValid on the validator. The Validator does that to itself
automatically so your value will not be maintained. Instead, a
CustomValidator function requires that you set args.IsValid to true or
false.

Private Sub CustomValidator1_ServerValidate(ByVal source As System.Object,
ByVal args As
System.Web.UI.WebControls.ServerValidateEventArgs) Handles
CustomValidator1.ServerValidate

If RadioButton1.Checked Then
args.IsValid = TextBox1.Text.Trim().Length > 0
Else
args.IsValid = False ' not checked. Always valid
End If
End Sub

FYI: Having one control enable a validator is a common problem but always
requires custom coding. I wrote a replacement to Microsoft's validators that
doesn't require custom coding. The 25 validators of Professional Validation
And More (http://www.peterblum.com/vam/home.aspx) have the Enabler property
where you define a rule that enables the validator based on the state of
other controls. It generates the correct client-side code and even supports
many more browsers than Microsoft's validators do in ASP.NET 1.x.

For example, my RequiredTextValidator with the Enabler set to monitor
radiobutton1 is checked looks like this:
<vam:RequiredTextValidator id=RTV1 runat=server ErrorMessage="Required"
ControlIDToEvaluate="TextBox1">
<EnablerContainer>
<vam:CheckStateCondition ControlIDToEvaluate="radiobutton1"
Checked="True" />
</EnablerContainer>
</vam:RequiredTextValidator>

--- Peter Blum
www.PeterBlum.com
Email: PL****@PeterBlum.com
Creator of "Professional Validation And More" at
http://www.peterblum.com/vam/home.aspx

"Mattyw" <wi********@hotmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
Hi,

I'm relatively new to Web Forms, I have been using Required Field
Validators and Regular Expression Validators on a Web Form I am
developing and everything works as expected using Visual Studio.NET
2003 and VB code behind.

I have a radio button and textbox, and basically i need the textbox to
be required based on the user selecting "Yes" from the radio button.
I've read through the newsgroups and found that a Custom Validator
could achieve this result, but am unsure how to implement it.

I have added a Custom Validator then a Required Field Validator to be
fired should the radiobutton be "Yes" and included it in the codebehind
page in the Custom Validator as below:

Private Sub CustomValidator1_ServerValidate(ByVal source As
System.Object, ByVal args As
System.Web.UI.WebControls.ServerValidateEventArgs) Handles
CustomValidator1.ServerValidate

if radiobutton.selectedvalue.text = "Yes"

then requiredfield.isvalid = true
else
requiredfield.isvalid = false

End if

End Sub
When I run the above the required field validator runs whatever is
checked by the radio button, so I'm obviously missing something or am I
thinking too logically???. I assumed that the above code would make the
required field validator "true" if "Yes" was selected and then display
the "error" and the validator would not fire based on "no" being
selected.

Any help would be grateful...I have a suspicion I'm going in the wrong
direction!!.

Cheers
Mattyw

Nov 19 '05 #4
Peter

Thanks I'll give it a go and let you know.

Peter Blum wrote:
The logic is wrong.

1. If the RadioButton is not checked, the validator should report no error.
It should return IsValid=true.
2. If the RadioButton is checked, test the value of the TextBox for text.
3. Do not set IsValid on the validator. The Validator does that to itself
automatically so your value will not be maintained. Instead, a
CustomValidator function requires that you set args.IsValid to true or
false.

Private Sub CustomValidator1_ServerValidate(ByVal source As System.Object,
ByVal args As
System.Web.UI.WebControls.ServerValidateEventArgs) Handles
CustomValidator1.ServerValidate

If RadioButton1.Checked Then
args.IsValid = TextBox1.Text.Trim().Length > 0
Else
args.IsValid = False ' not checked. Always valid
End If
End Sub

FYI: Having one control enable a validator is a common problem but always
requires custom coding. I wrote a replacement to Microsoft's validators that
doesn't require custom coding. The 25 validators of Professional Validation
And More (http://www.peterblum.com/vam/home.aspx) have the Enabler property
where you define a rule that enables the validator based on the state of
other controls. It generates the correct client-side code and even supports
many more browsers than Microsoft's validators do in ASP.NET 1.x.

For example, my RequiredTextValidator with the Enabler set to monitor
radiobutton1 is checked looks like this:
<vam:RequiredTextValidator id=RTV1 runat=server ErrorMessage="Required"
ControlIDToEvaluate="TextBox1">
<EnablerContainer>
<vam:CheckStateCondition ControlIDToEvaluate="radiobutton1"
Checked="True" />
</EnablerContainer>
</vam:RequiredTextValidator>

--- Peter Blum
www.PeterBlum.com
Email: PL****@PeterBlum.com
Creator of "Professional Validation And More" at
http://www.peterblum.com/vam/home.aspx

"Mattyw" <wi********@hotmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
Hi,

I'm relatively new to Web Forms, I have been using Required Field
Validators and Regular Expression Validators on a Web Form I am
developing and everything works as expected using Visual Studio.NET
2003 and VB code behind.

I have a radio button and textbox, and basically i need the textbox to
be required based on the user selecting "Yes" from the radio button.
I've read through the newsgroups and found that a Custom Validator
could achieve this result, but am unsure how to implement it.

I have added a Custom Validator then a Required Field Validator to be
fired should the radiobutton be "Yes" and included it in the codebehind
page in the Custom Validator as below:

Private Sub CustomValidator1_ServerValidate(ByVal source As
System.Object, ByVal args As
System.Web.UI.WebControls.ServerValidateEventArgs) Handles
CustomValidator1.ServerValidate

if radiobutton.selectedvalue.text = "Yes"

then requiredfield.isvalid = true
else
requiredfield.isvalid = false

End if

End Sub
When I run the above the required field validator runs whatever is
checked by the radio button, so I'm obviously missing something or am I
thinking too logically???. I assumed that the above code would make the
required field validator "true" if "Yes" was selected and then display
the "error" and the validator would not fire based on "no" being
selected.

Any help would be grateful...I have a suspicion I'm going in the wrong
direction!!.

Cheers
Mattyw


Nov 19 '05 #5
Hi All,

It worked fine, I created a small test App and set the args.isvalid to
True and it does what
I want....so thanks for that.

However once I put it in the actual application it won't fire, the code
is the same as in the test!.I've got a button click that fires to
perform a calculation, when i click the button all the other validation
on the form works as expected except the custom validation, i've tried
testing it by just having it write to a label and textbox but still no
luck.

I'm a bit confused as to why it won't fire when it worked fine in the
test form.

Is there anywhere the custom validator should be in the code behind
page?, I just dragged the custom validator on to the page and then
double clicked it to get to the code behind....I don't have any other
custom validation in my form....I'm wondering if something has got out
of step?

Thanks
Matt.

Nov 19 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by john | last post: by
5 posts views Thread by | last post: by
9 posts views Thread by wardy1975 | last post: by
reply views Thread by Marek | last post: by
3 posts views Thread by Andy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.