0

I have six input fields in form and I need to do some validation to make sure that none of them are identical in real time. For example if a user try's to enter a duplicate field jQuery would prompt the user that they must enter unique fields, before even submitting the form. I assume that I could use a 'keyup' function but I'm not sure. I'm trying to use jQuery validate but can only get it working for two fields at a time. Is this possible with jQuery or maybe there is a better solution? Angular?

2 Answers 2

1

Give all the fields a class like class="alldifferent". Then you can use:

$(".alldifferent").keyup(function() {
    var val = $(this).val();
    if (val != '') {
        $(".alldifferent").not(this)).each(function() {
            if ($(this).val() == val) {
                alert("Error, duplicate value " + val);
                return false; // stop the loop
            }
        });
    }
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thank You! exactly what I was looking for.
0

The Jquery Validation work on submit from you can use keyup function see in documentation

https://jqueryvalidation.org/equalTo-method

1 Comment

This seems to be the opposite of what he wants.

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.