views:

67

answers:

3

hello,

how can I make a checkbox required ?

my code

aspx:

<asp:CheckBox ID="cbIsAgree" CssClass="{CheckedBox:true, messages:{CheckedBox:'check me!.'}}" runat="server" Text="check me!" />

javascript :

$.validator.addMethod("CheckedBox", function CheckedBox(value, element) {
        return $(element).is(':checked');
    }
    , 'check me!');

that code isnt working ... why ?

A: 

add required:true to the cssClass thing

e.g. in normal html

<input name="user" title="enter" class="{required:true,minlength:3}" />
jitter
I'm looking for example on Asp.net Checkbox to be required .... this doent work ... <asp:CheckBox ID="CheckBox1" CssClass="{required:true, messages:{required:'check me!'}}" runat="server" Text="check me!" />
haddar
A: 

I think you usage of the validator is wrong. All the addMethod function does is add a validation method. It does not bind that validation to the actual input for validation.

It is a two step process:

  • Define a method that says how an element should be validated
  • Create a list of rules that link an input to the relevant validation method

See the example on this page:

http://docs.jquery.com/Plugins/Validation/Methods/required

Michael Edwards
I'm looking for example on Asp.netCheckbox to be required ....this doent work ...<asp:CheckBox ID="CheckBox1" CssClass="{required:true, messages:{required:'check me!'}}" runat="server" Text="check me!" />
haddar
A: 
 $(document).ready(function() {
        $("#<%=cbIsAgree.ClientID %>").addClass("{required:true, messages:{required:'check me !.'}}");

    });

this will work cause .net puts the class in the span if you write something like this

<asp:chaeckbox runat="server" class=".....
haddar