jQuery Validation checkbox custom value rule

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.

Answers


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
        }
    }
});

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); 

});

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!


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/


Need Your Help

How can you limit the value from input using AngularJS?

angularjs angularjs-directive

I am looking for ways to limit the value inside the input to 4 and process the 4 digit value unto my controller.

Using Razor Templates to produce a .aspx page

c# razor tridion

I am trying to use a Razor template to produce a .aspx page as output. I'm not having luck looking in the documentation for how to do this. The Page and namespace declarations are breaking the temp...