2

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

1 Answer 1

2

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
});
Sign up to request clarification or add additional context in comments.

2 Comments

Right! Sorry about that. Edited.
Just keep on adding to the r object. e.g. r[ elM ] = { equalTo: elN };

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.