6

Here is my simple validation code for testing:

    jQuery.validator.addMethod("oneTypeChecked", function (value, element) {
        return false; //$(':checkbox:checked') > 0
    }, "Check one or more type");

    $("#RequestCreateForm").validate({
        rules: {
            ChkBoxType1: { oneTypeChecked: true }
        }
    });

But my method oneTypeChecked is never called when I submit form. Why?

Here is my HTML code:

<div class="editor-field">
    <input type="checkbox" id="ChkBoxType2" name="requestTypeIds2" value="2">Type 2     
    <input type="checkbox" id="ChkBoxType1" name="requestTypeIds1" value="1">Type 1        
    <input type="checkbox" id="ChkBoxType3" name="requestTypeIds3" value="3">Type 3
</div>

Thank you very much!

3
  • can you also post your html markup? is it possible that there isn't an element called 'ChkBoxType' in your markup? Commented May 16, 2011 at 16:04
  • Validation is not standard out of the box -- which plugin are you using. Commented May 16, 2011 at 16:15
  • @Hogan: I'm using jQuery validation plug-in 1.6 Commented May 16, 2011 at 16:19

3 Answers 3

8

validation plugin works according to the name attribute and not id. try changing the name attribute on your input.

Sign up to request clarification or add additional context in comments.

Comments

2

with this html

<form id="RequestCreateForm" action="" method="post">
    <div class="editor-field">
        <input type="checkbox" class="oneTypeChecked" name="requestTypeIds2" value="2">Type 2     
        <input type="checkbox" class="oneTypeChecked" name="requestTypeIds1" value="1">Type 1       
        <input type="checkbox" class="oneTypeChecked" name="requestTypeIds3" value="3">Type 3
    </div>
    <input type="submit" value="submit" />
</form>

and this jquery

jQuery.validator.addMethod("oneTypeChecked", function(value, element) {
    return $('.oneTypeChecked:checked') > 0
}, "Check one or more type");

$("#RequestCreateForm").validate({
    rules: {
        ChkBoxType1: {
            oneTypeChecked: true
        }
    }
});

you will get the result you want

here is a WORKING DEMO

4 Comments

after hours of seraching why my custom validation doesn't work, your solution finally showed me, I hadn't added the method name to the class of the input. thanks! None of the other examples show this crucial step
I'm happy this helped you, have a great day
Thanks.. spent more than 4 hours on this issue, and finally it came down to this! Thanks Man.. this is absolute brilliant
glad i could help
0

As I understand it, if you don't add in the required it will be valid because it is not checked.

    rules: {   
        ChkBoxType1: { required : true, oneTypeChecked: true }
    }

1 Comment

No, what is required is that at least one checkbox must be required, not only the 'ChkBoxType1'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.