473,513 Members | 2,749 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2914
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2140
by: john | last post by:
Hello, One our thrid page for some reason the response.redirect isn't working on the live server. It works fine on the development machine but when we move the code to the live server it doesn't...
2
3705
by: Barbara Alderton | last post by:
I setup some standard Required Field Validation controls and one Custom validation control on an ASP.NET page (within a user control) to validate text entry. I also setup a Summary Control to post...
1
2416
by: Stephen Adam | last post by:
Hi there, I have written a custom validation control which checks to see of an input field is not empty and contains only numeric data. I was using a regular expression validation control but...
5
2627
by: | last post by:
Hi all, Has anyone been able to write some custom javascript on the onclick event of submit button to do certain things like disable submit button, only submit form once etc. This was a breeze...
9
3123
by: wardy1975 | last post by:
Hi All, Looking for a little expert advice on a few web standards issues. I am currently trying to understand the impact of web standards for a web application I work with. I have been doing a...
0
1041
by: Marek | last post by:
Hi all, I have custrom control with four elements: text box, regular expression validator, required field validator and custom validator. Next this control is dragged on to web site with several...
3
8233
by: Andy | last post by:
Hi folks, I have a customvalidator control that works properly if it isn't contained in an ASP:TABLE. But, when I place it inside an ASP:TABLE, I find that _ServerValidate doesn't get fired,...
3
3390
by: matko | last post by:
This is a long one, so I'll summarize: 1. What are your opinions on raising an exception within the constructor of a (custom) exception? 2. How do -you- validate arguments in your own...
1
3770
gagandeepgupta16
by: gagandeepgupta16 | last post by:
Hi I am working on an entry form using validation controls in ASP.NET. I have two controls which requires custom validators, no issue in using plain custom validators. But when i am using...
0
7259
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7380
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
7535
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5683
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
4745
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3232
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3221
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1592
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
455
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.