midstack midstack - 7 months ago 65
Javascript Question

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

AVK AVK
Answer

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