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

Question

Question

Show error message if value has already been entered into a table

asked on July 26, 2017

Hi,

I would like to display an error message if a value has already been entered into a column within a table to ensure there are no duplicate entries. How would I go about this? Here is my table:

So I want to ensure that they aren't entering the same Lot # twice. I'm not sure how I would do this in Javascript and would really appreciate some help. Let me know if you need anything further.

 

Thanks

0 0

Answer

SELECTED ANSWER
replied on July 27, 2017

Sorry I forgot to mention that code was for 10.2 and above. Forms validation library changed from Webshim to Parsley since 10.2.

You can try the following on Forms 9.2:

$(document).ready(function(){
  register();
  $('.ObjectivesTable').on('change', '.LN input', function(){
    $('.LN input').trigger('updatevalidation');
  });
  $('.ObjectivesTable .cf-table-add-row').click(register);

  function register()
  {
    $('.LN input').on('valuevalidation', function (e, value) {
      if (value) {
        var valueList = $('.LN input').map(function() {return $(this).val(); }).get();
        return (_.filter(valueList, function(v) { return v == value.value }).length == 1) ? false :  'Lot number should be unique.'
      }
    });
  }
});

 

1 0

Replies

replied on July 26, 2017

Hi David,

You can try the script below (I used CSS name the same as yours on the screenshot:) 

$(document).ready(function(){
  register();
  $('.ObjectivesTable .cf-table-add-row').click(register);
  window.Parsley.addValidator('uniqueln', {
    validateString: function(value) {
      var valueList = $('.LN input').map(function() {return $(this).val(); }).get();
      return _.filter(valueList, function(v) { return v == value }).length == 1;
    },
    messages: {
      en: 'Lot number should be unique.',
    }
  });
  function register()
  {
    $('.LN input').attr('data-parsley-uniqueln', '');
    $('.LN input').change(function(){
      $('.LN input').each(function(index, el){$(el).parsley().validate();})
    });
  }
});

It will show error alert on fields that have duplicated values, and will update error alert on all fields once any of the field changes.

1 0
replied on July 27, 2017 Show version history

Rui,

 

Thanks for the response. I added this code, but for some reason it's not working and I honestly have no idea why. Any thoughts? I went ahead and made a simple test form with just a table in it:

The table has the class "table" and Column 1 has the class "col1". I changed your code to account for these differences: 

$(document).ready(function(){
  register();
  $('.table .cf-table-add-row').click(register);
  window.Parsley.addValidator('uniqueln', {
    validateString: function(value) {
      var valueList = $('.col1 input').map(function() {return $(this).val(); }).get();
      return _.filter(valueList, function(v) { return v == value }).length == 1;
    },
    messages: {
      en: 'Lot number should be unique.',
    }
  });
  function register()
  {
    $('.col1 input').attr('data-parsley-uniqueln', '');
    $('.col1 input').change(function(){
      $('.col1 input').each(function(index, el){$(el).parsley().validate();})
    });
  }
});

 

Thanks

 

edit: I'm on Forms 9.2 for now, not sure if that matters.

0 0
SELECTED ANSWER
replied on July 27, 2017

Sorry I forgot to mention that code was for 10.2 and above. Forms validation library changed from Webshim to Parsley since 10.2.

You can try the following on Forms 9.2:

$(document).ready(function(){
  register();
  $('.ObjectivesTable').on('change', '.LN input', function(){
    $('.LN input').trigger('updatevalidation');
  });
  $('.ObjectivesTable .cf-table-add-row').click(register);

  function register()
  {
    $('.LN input').on('valuevalidation', function (e, value) {
      if (value) {
        var valueList = $('.LN input').map(function() {return $(this).val(); }).get();
        return (_.filter(valueList, function(v) { return v == value.value }).length == 1) ? false :  'Lot number should be unique.'
      }
    });
  }
});

 

1 0
replied on July 28, 2017

Sweeeet! Thank you. This is exactly what I needed.

0 0
replied on July 27, 2017

Hi Rui, 

Is there a parsley printer you can recommend? 

 

-Ben 

0 0
replied on July 27, 2017

Hi Ben, hmm, what is a parsley "printer"?

0 0
replied on July 28, 2017

Oops, I meant "primer."

0 0
replied on July 30, 2017

Of course it's the official doc http://parsleyjs.org/doc/index.html

There is a demo in the custom validator part which I use most of the time. The events part is also helpful for triggering validation on demand.

1 0
replied on July 31, 2017

Thanks - it's always good to get a recommendation from someone who has used the documentation.

0 0
replied on November 30, 2020

I can't get this to work for my table.  I'm in Laserfiche 10.4

0 0
replied on November 30, 2020

Which field would you like to check for duplicates?

The code above did not work for drop-down fields.

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

Sign in to reply to this post.