midstack
midstack

Reputation: 2123

How can I use variable for jquery validation equalTo method?

I want to use variable for jquery validation equalTo method. But not working for jquery selector. How can I do this?

HTML

<form id="form" method="post">
    <div>
        <input type="password" name="pass1" id="passM" required />
    </div>
    <div>
        <input type="password" name="pass2" id="passR" required />
    </div>
    <button type="submit">Submit</button>
</form>

JS

var form = $('#form'),
    el1  = '#'+form.find('#passM').attr('id'),
    el2  = form.find('#passR').attr('name');


form.validate({

    rules: {

          el2: {
            equalTo: el1
        }
    }
})

Code example

Upvotes: 2

Views: 1686

Answers (1)

AVK
AVK

Reputation: 645

Would this work?

var form = $('#form'),
    el1  = '#'+form.find('#passM').attr('id'),
    el2  = form.find('#passR').attr('name');

var r = {};
r[el2] = { equalTo: el1 };

form.validate({
    rules: r
});

Upvotes: 2

Related Questions