473,732 Members | 2,217 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 CustomValidator 1_ServerValidat e(ByVal source As
System.Object, ByVal args As
System.Web.UI.W ebControls.Serv erValidateEvent Args) Handles
CustomValidator 1.ServerValidat e

if radiobutton.sel ectedvalue.text = "Yes"

then requiredfield.i svalid = true
else
requiredfield.i svalid = 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 2925
I think maybe what you mean to have is this:

if radiobutton.sel ectedvalue.text = "Yes"

then requiredfield.e nabled = true
else
requiredfield.e nabled = 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 CustomValidator 1_ServerValidat e(ByVal source As
System.Object, ByVal args As
System.Web.UI.W ebControls.Serv erValidateEvent Args) Handles
CustomValidator 1.ServerValidat e

if radiobutton.sel ectedvalue.text = "Yes"

then requiredfield.i svalid = true
else
requiredfield.i svalid = 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 CustomValidator 1_ServerValidat e(ByVal source As System.Object,
ByVal args As
System.Web.UI.W ebControls.Serv erValidateEvent Args) Handles
CustomValidator 1.ServerValidat e

If RadioButton1.Ch ecked Then
args.IsValid = TextBox1.Text.T rim().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 RequiredTextVal idator with the Enabler set to monitor
radiobutton1 is checked looks like this:
<vam:RequiredTe xtValidator id=RTV1 runat=server ErrorMessage="R equired"
ControlIDToEval uate="TextBox1" >
<EnablerContain er>
<vam:CheckState Condition ControlIDToEval uate="radiobutt on1"
Checked="True" />
</EnablerContaine r>
</vam:RequiredTex tValidator>

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

"Mattyw" <wi********@hot mail.com> wrote in message
news:11******** **************@ g44g2000cwa.goo glegroups.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 CustomValidator 1_ServerValidat e(ByVal source As
System.Object, ByVal args As
System.Web.UI.W ebControls.Serv erValidateEvent Args) Handles
CustomValidator 1.ServerValidat e

if radiobutton.sel ectedvalue.text = "Yes"

then requiredfield.i svalid = true
else
requiredfield.i svalid = 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 CustomValidator 1_ServerValidat e(ByVal source As System.Object,
ByVal args As
System.Web.UI.W ebControls.Serv erValidateEvent Args) Handles
CustomValidator 1.ServerValidat e

If RadioButton1.Ch ecked Then
args.IsValid = TextBox1.Text.T rim().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 RequiredTextVal idator with the Enabler set to monitor
radiobutton1 is checked looks like this:
<vam:RequiredTe xtValidator id=RTV1 runat=server ErrorMessage="R equired"
ControlIDToEval uate="TextBox1" >
<EnablerContain er>
<vam:CheckState Condition ControlIDToEval uate="radiobutt on1"
Checked="True" />
</EnablerContaine r>
</vam:RequiredTex tValidator>

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

"Mattyw" <wi********@hot mail.com> wrote in message
news:11******** **************@ g44g2000cwa.goo glegroups.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 CustomValidator 1_ServerValidat e(ByVal source As
System.Object, ByVal args As
System.Web.UI.W ebControls.Serv erValidateEvent Args) Handles
CustomValidator 1.ServerValidat e

if radiobutton.sel ectedvalue.text = "Yes"

then requiredfield.i svalid = true
else
requiredfield.i svalid = 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
2154
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 work. The first page is a login page and the response.redirect does work there but not on the third page. Any ideas? Thanks in advance. John
2
3733
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 all the messages to a message box (ShowMessageBox=true). The required field validation error messages show up in the summary just fine but I can't get the custom validation message to show up if invalid. So far I have the summary control...
1
2429
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 was unable to get it fail if a field was blank. My problem now is that while my custom validation control will detect if a field matches my requirement and will display a error message if it doesnt, it wont stop it from being used and sent to my...
5
2646
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 in 1.1 since I could edit the .js file. Now in 2.0 I can no longer do this. Also, my code would have to be called after all client-side validation was done and was successful. Any ideas? TIA!
9
3137
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 lot of research in the areas of XHTML and WAI compliance, and am attempting to come up with a recommendation for our product in terms of standards level compliance. Ideally, I would like to be at XHTML 1.0 Strict. However, in my reading I have...
0
1061
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 controls. All controls has the same validation group. When I press button for this validation group then all validations runs ok except custom validation. When this push button is removed from validation group then custom validation runs ok but...
3
8249
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, even if I force the customvalidator to validate by invoking its validate() method after a button is clicked: cvCustomValidator_ServerValidate(object source, system.Web.UI.WebControls.ServerValidateEventArgs args) <== doesn't get
3
3406
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 exception constructors? I've noticed that, f.ex., ArgumentException accepts null arguments without raising ArgumentNullException. Obviously, if nothing is to be supplied to the exception constructor, the default constructor should
1
3791
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 Validation Callout Extender with the custom validators its not working properly. here is the link where i got solution for one custom validator with validation callout extender :- http://www.junnark.com/Articles/Article0001.aspx I searched a lot but...
0
8946
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9447
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8186
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6735
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6031
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4550
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4809
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2180
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.