You are viewing limited content. For full access, please sign in.

Question

Question

Forms field value duplicate validation

asked on March 29, 2021

I have two fields (Maximum budget allowance and Total Funds) and am trying to add validation to them so that they must match. The Total Funds field is a read only total of all the funds fields above it. I am trying to use the same logic from this post: https://answers.laserfiche.com/questions/155386/Javascript-compare-multiple-fields#155415 

But I cannot get Maximum budget allowance field to revalidate when the different Funds fields are entered. When I manually delete the field and reenter the correct total, it then validates correctly. Here is the code:

"totalBudget" is the Maximum budget allowance field, "totalFunds" is the Total Funds field, "funds" is all the Funds fields.

$(document).ready(function(){
  $(".totalBudget input").attr('data-parsley-duplicate-checker','.totalFunds');
$(".funds input").attr('data-parsley-trigger','focusout');
$(".funds input").attr('data-parsley-trigger-after-failure','input');
window.Parsley.addValidator("duplicateChecker", {
    validateString: function(fieldValue) {
        let values = $(".totalFunds input").map(function() { return $(this).val(); });
        let existing = _.filter(values, function(value) {
            return fieldValue == value;
        });
        return existing.length == 1;
    },
    messages: {
        en: "Total Funds must match budget"
    }
  });
});

Please help me!

0 0

Answer

SELECTED ANSWER
replied on March 29, 2021

You can try replacing lines 3 and 4 with this: 

  $(".funds input").change(function() {
    $('.totalBudget input').parsley().validate();
  });

 

This says whenever any of the fields with the Class Name of funds is changed, re-validate the parsley on the field with the class name of totalBudget.

0 0
replied on March 29, 2021

That was it, thank you Matthew!

1 0
replied on March 29, 2021

You are quite welcome, glad to help. smiley

0 0

Replies

You are not allowed to reply in this post.
You are not allowed to follow up in this post.

Sign in to reply to this post.