2
  1. I am trying to add a custom method/rule to JQuery Validator.
  2. This method/rule will be applied to a array of text boxes that are generated dynamically.
  3. After creating an array of values and validating i get a json with info like this {error: false, duplicate: true, exists: false}
  4. Now if it is duplicate I want to show message "Duplicate", if it exists - "Exists on Server" or "Some Error Occurred". [if multiple messages not possible, then single message]
  5. When I do a return from inside $.post() it does not effect the validator method, validator returns false even if I return true inside $.post()

My Current Rule/Method

$.validator.addMethod("duplicate_vc", function (value, element) {
            var vc_arr = $("input[name='vc[]']")
                    .map(function () {
                        return $(this).val();
                    }).get();
            //console.log(vc_arr);
            $.post("forms/ajax_call.php", {func: "check_vc", data: vc_arr}, function (data) {
                console.log(data);
                //everything inside ajax is correct
                //return statements do not work
                if(data.duplicate || data.exists){
                    return false;
                }else{
                    console.log('true;');
                    return true;
                }
            }, "json");
        }, "Duplicate VC Found. ");

I have seen these with not much help.

jQuery validation custom rule
jQuery validator and a custom rule that uses AJAX
jquery-validate - addMethod - how to apply custom rule referencing two text boxes?

Community
  • 1
  • 1
Rohit Hazra
  • 657
  • 9
  • 27

0 Answers0