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

Question

Question

Javascript compare multiple fields

asked on February 15, 2019

I have a form with multiple approvers, who's names are entered at the bottom of the form for dynamic routing. I am looking to validate these fields against each other just to make sure no duplicates are present before the user submits. Any ideas on the best way to accomplish this?

0 0

Answer

SELECTED ANSWER
replied on February 15, 2019 Show version history

You can use the following JavaScript.

$(document).ready(function(){
  $(".no-dupe input").attr('data-parsley-duplicate-checker','');
  $(".no-dupe input").attr('data-parsley-trigger','focusout');
  $(".no-dupe input").attr('data-parsley-trigger-after-failure','focusout');
  
  window.Parsley.addValidator("duplicateChecker", {
	requirementType: "string",
	validateString: function(fieldValue) {
		let values = $(".no-dupe input").map(function() { return $(this).val(); });
        let existing = _.filter(values, function(value) {
            return fieldValue == value;
        });
      
    	return existing.length == 1;
	},
	messages: {
		en: "You cannot have duplicate approvers"
	}
  });
});

Just put a no-dupe custom CSS class on each of the fields you want to be unique.

2 0
replied on February 15, 2019

This isn't needed in this case, but I've recently started using ":input" instead of just "input" because it catches input, select, textarea, and button elements. 

1 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.