Validating a checkbox

G

Guest

I'm having a problem which I thought would be a very simple thing but is
turning out to be a difficult challenge. I have a web form with several input
fields and I have a single checkbox, not a checkbox list. Before the form can
be submitted I want to require that the checkbox be checked. Sounds simple
but non of the validation controls seem to work correct for this, or maybe
I'm just not using them correctly. I have done several searches but I can't
seem to find any examples of this type of thing being done. Which is very
surprising because this sort of checkbox is a very common thing. i.e. "I
agree to the terms....." Can anyone point me to an example? I'm using ASP.NET
1.1. Thanks
 
G

Guest

You will have to iterate through the controls collection, checking each one
for the type (CheckBox) and then if it's Checked.... till you get to the end
or till you get a positive on the Checked...
 
E

Emil Christopher Melar

Mike said:
I'm having a problem which I thought would be a very simple thing but is
turning out to be a difficult challenge. I have a web form with several input
fields and I have a single checkbox, not a checkbox list. Before the form can
be submitted I want to require that the checkbox be checked. Sounds simple
but non of the validation controls seem to work correct for this, or maybe
I'm just not using them correctly. I have done several searches but I can't
seem to find any examples of this type of thing being done. Which is very
surprising because this sort of checkbox is a very common thing. i.e. "I
agree to the terms....." Can anyone point me to an example? I'm using ASP.NET
1.1. Thanks

This is very easy.

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;

namespace MyNameSpace.MyControls
{
/// <summary>
///
/// </summary>
[ValidationProperty("ValidationValue")]
public class ValidatableCheckBox : CheckBox
{
public string ValidationValue
{
get
{
return ( this.Checked ) ? "True" : string.Empty;
}
}

}
}


This will trig the RequiredFieldValidator, since it looks for the
ValidationProperty attribute. If not checked, the value of the
ValidationProperty's Field Reference (ValidationValue), will return
String.Empty ("") when it is not checked. Therefore it will be Invalid
since the RequiredFieldValidator will trig on an empty string (since
that's the default initial value on the validator)!

Use this class (subclass of CheckBox) in the aspx, and attach a
RequiredFieldValidator to it.

Alternatively, you don't need the ValidationValue property, you can just
set ValidationProperty attrib to "Checked", then set InitialValue on the
validator to "False".

Good luck!
 
G

Guest

Thank you for your response. That solved my problem.

Emil Christopher Melar said:
Mike said:
I'm having a problem which I thought would be a very simple thing but is
turning out to be a difficult challenge. I have a web form with several input
fields and I have a single checkbox, not a checkbox list. Before the form can
be submitted I want to require that the checkbox be checked. Sounds simple
but non of the validation controls seem to work correct for this, or maybe
I'm just not using them correctly. I have done several searches but I can't
seem to find any examples of this type of thing being done. Which is very
surprising because this sort of checkbox is a very common thing. i.e. "I
agree to the terms....." Can anyone point me to an example? I'm using ASP.NET
1.1. Thanks

This is very easy.

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;

namespace MyNameSpace.MyControls
{
/// <summary>
///
/// </summary>
[ValidationProperty("ValidationValue")]
public class ValidatableCheckBox : CheckBox
{
public string ValidationValue
{
get
{
return ( this.Checked ) ? "True" : string.Empty;
}
}

}
}


This will trig the RequiredFieldValidator, since it looks for the
ValidationProperty attribute. If not checked, the value of the
ValidationProperty's Field Reference (ValidationValue), will return
String.Empty ("") when it is not checked. Therefore it will be Invalid
since the RequiredFieldValidator will trig on an empty string (since
that's the default initial value on the validator)!

Use this class (subclass of CheckBox) in the aspx, and attach a
RequiredFieldValidator to it.

Alternatively, you don't need the ValidationValue property, you can just
set ValidationProperty attrib to "Checked", then set InitialValue on the
validator to "False".

Good luck!
 

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,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top