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

Question

Question

Javascript to alert when fields are not the same

asked on January 29, 2019

In our leave request process, we need for leave request dates to all be in the same pay period. Request dates (Date of Leave) are entered in a table, and the pay period dates (PPE) are populated from a lookup.

I'd like to find a way to alert users when additional dates entered in the table do not match the PPE of the first entry, or the PPE dates do not all match each other. I started to cobble together javascript based on information in this post but obviously I am missing something:

 

$(document).ready(function(){
  register();
  $('.RequestTypeDetails').click(register);
  window.Parsley.addValidator('notequalto', {
    validateString: function(value) {
      var valueLookup = $('.PPEdate change').map(function() {return $(this).val(); }).get();
      return _.filter(valueLookup, function(v) { return v !== value }).length == 1;
    },
    messages: {
      en: 'Leave dates must be in the same pay period!',
    }
  });
  function register()
  {
    $('.PPEdate change').attr('data-parsley-notequalto', '');
    $('.PPEdate change').change(function(){
      $('.PPEdate change').each(function(index, el){$(el).parsley().validate();})
    });
  }
});

 

Thank you for any assistance.

1 0

Replies

replied on January 30, 2019

I made slight progress today by changing the event to "input" instead of "change."

$(document).ready(function(){
  register();
  $('.RequestTypeDetails').click(register);
  window.Parsley.addValidator('notequalto', {
    validateString: function(value) {
      var valueLookup = $('.PPEdate input').map(function() {return $(this).val(); }).get();
      return _.filter(valueLookup, function(v) { return v !== value }).length == 1;
    },
    messages: {
      en: 'Leave dates must be in the same pay period!',
    }
  });
  function register()
  {
    $('.PPEdate input').attr('data-parsley-notequalto', '');
    $('.PPEdate input').change(function(){
      $('.PPEdate input').each(function(index, el){$(el).parsley().validate();})
    });
  }
});

 

But what happens now is that the message appears from the get-go:

If I add a date in a different pay period, the message then only appears on the errant date:

I've experimented with other scenarios - adding a date in yet a third pay period, deleting rows, etc., all with mixed results. I'm thinking that ultimately the best thing is to show the alert message only if the PPE date in the first row is not matched in all other rows...? I'd appreciate some advice on that.

Thank you.

0 0
replied on February 20, 2019

Still looking for assistance with this question - thank you.

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

Sign in to reply to this post.