Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement "or" condition in Jquery validation rules

I am currently using jquery validation plugin to validate my form, here i want to validate Landline number and mobile number .User want to enter any one.So I Want to use Or condition in jquery validation rules.

rules{ 'landline':{ required:true }, 'mobile':{ required:true } }

How to make any one field mandatory.

like image 712
user1665624 Avatar asked Oct 27 '25 20:10

user1665624


1 Answers

If you want the error messages to properly toggle and keep your code looking concise, just use the method that was created specifically for this situation. The rule/method is called require_from_group and included in the additional-methods.js file.

$(document).ready(function () {

    $("#form").validate({
        rules: {
            landline: {
                require_from_group: [1, '.phone'],
                number: true
            },
            mobile: {
                require_from_group: [1, '.phone'],
                number: true
            }
        }
    });

});

DEMO: http://jsfiddle.net/t45dc/

And to optionally combine both error messages into one, use the groups option...

$(document).ready(function () {

    $("#form").validate({
        rules: {
            landline: {
                require_from_group: [1, '.phone'],
                number: true
            },
            mobile: {
                require_from_group: [1, '.phone'],
                number: true
            }
        },
        groups: {
            phones: 'landline mobile'
        }
    });

});

DEMO: http://jsfiddle.net/t45dc/1/

like image 93
Sparky Avatar answered Oct 30 '25 19:10

Sparky