0

Is it possible to check for a custom value (besides True / False) on a checkbox using the jQuery Validation plugin?

For example :

<input id="test" type="checkbox" name="test" value="something">

I cannot find a way to check for 'something' being the value. For specific reasons I cannot just use 'true' as the value.

5
  • Thanks to the answerers. But is there a specific rule to use in jQuery validation plugin, which I can't seem to find in the docs (jqueryvalidation.org/documentation) Commented Jan 31, 2014 at 13:44
  • I have posted the answer as your requirement. Commented Jan 31, 2014 at 13:48
  • @Josh I have updated my answer jsfiddle.net/aJBK9/2 Commented Jan 31, 2014 at 13:58
  • Thank you to all the good answers. Commented Jan 31, 2014 at 14:13
  • Josh – mark an answer as correct Commented Jan 31, 2014 at 14:28

4 Answers 4

1

Try this :

$('#test').attr('value');

Demo JSFiddle

You can use it inside custo validation method.

To declare custom rule :

$.validator.addMethod("myRule", function(value, element) {
    var v = element.attr('value');
    if(v === 'something') {
        return true;
    }
    return true;
}, $.validator.format('my message') );

To set the custom rule on a field :

$('#form').validate({
    rules : {
        test : {
            myRule : true
        }
    }
});
Sign up to request clarification or add additional context in comments.

Comments

1

Use:

$("#test").val()

or if you want to get value in change event then:

demo : http://jsfiddle.net/qsDn5/16/

$("#test").change(function(){
   alert(this.value); 

});

1 Comment

Thanks but how would I use this as a rule for jQuery validation plugin (jqueryvalidation.org/documentation)
1

You can add the rule using the addMethod which then returns a boolean based on the result of a test like so:

// Add Custom Method
$.validator.addMethod('someTest', function(value, element){
    return 'something' === value;
}, $.validator.format('Custom Message'));

// Use Custom Method
$('#theForm').validate({
    rules: {
        checkBoxField: {
            someTest: true
        }
    ...
});

JSFiddle Demo.

I hope this helps!

Comments

0

Try Jquery Validation plugin,

<form id="myform">
    <input type="checkbox" name="test[]" />x
    <input type="checkbox" name="test[]" />y
    <input type="checkbox" name="test[]" />z
    <input type="submit" />
</form>
<a id="docs" href="http://docs.jquery.com/Plugins/Validation" target="_blank">Validation Documentation</a>

Code:

$(document).ready(function () {

    $('#myform').validate({ // initialize the plugin
        rules: {
            'test[]': {
                required: true,
                maxlength: 2
                custommethod: true
            }
        },
        messages: {
            'test[]': {
                required: "You must check at least 1 box",
                maxlength: "Check no more than {0} boxes"
            }
        },
        submitHandler: function (form) { // for demo
            alert('valid form submitted'); // for demo
            return false; // for demo
        }
    });

});

Update: http://jsfiddle.net/aJBK9/2/

Use the addMethod in jquery Validation,

$.validator.addMethod("custommethod", function(value, element) {
    if (value==="something"){return true;}
return false;

}, "Value must be some thing");

Check this demo link http://jsfiddle.net/aJBK9/1/

Comments

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.