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

Question

Question

Public Forms profanity filter

asked on June 27, 2022

Hello,

We have a public facing Laserfiche form with a multiline text field.
We would like to stop the submission of this form if there is any profanity in the multiline field.
Has anyone any experience of how we might achieve this?
Google tells me that there are javascript routines and publicly available "rude word" lists.
Any suggestions on to implement this?

Thank you.

1 0

Answer

SELECTED ANSWER
replied on June 28, 2022

Here's one way to do it.  Give your field a CSS Class of: profanityParsleyCheck

Add the following Javascript to your form, updating the list of words on line 11 (keep them lowercase).  Although my kids will tell you I swear like a sailor, I'm not going to put it down in writing on this site that I love to support, so I used words I hear people say who don't want to actually swear (but we all really know what they mean).  

$(document).ready(function() {
  
  //Add custom parsley validation to the field with the profanityParsleyCheck class
  //to return an error if it includes profanity.
  $('.profanityParsleyCheck input').attr('data-parsley-profanitycheck','');
  $('.profanityParsleyCheck textarea').attr('data-parsley-profanitycheck','');
  $('.profanityParsleyCheck select').attr('data-parsley-profanitycheck','');
  window.Parsley.addValidator('profanitycheck', {
  validateString: function(value) 
    {
      var profanity_array = new Array("gosh", "darn", "dagnabit");
      var regex = new RegExp('\\b(' + profanity_array.join('|') + ')\\b', 'i' );
      return (!regex.test(value)); 
    },
    messages: 
    {
      en: 'Field includes profanity, please resolve.'
    }
  });
  
});

 

End result:

3 0

Replies

replied on June 28, 2022

That's great - thank you.
I'm going to try and butcher this to enable/disable the submit button based on this.

1 0
replied on June 28, 2022 Show version history

Here's modified Javascript that also disables the Submit button (and lowers its opactity - alternately, you could show/hide it, but I personally like this method better) if the field with the profanityParsleyCheck class has any parsley errors: 

$(document).ready(function() {
  
  //Add custom parsley validation to the field with the profanityParsleyCheck class
  //to return an error if it includes profanity.
  $('.profanityParsleyCheck input').attr('data-parsley-profanitycheck','');
  $('.profanityParsleyCheck textarea').attr('data-parsley-profanitycheck','');
  $('.profanityParsleyCheck select').attr('data-parsley-profanitycheck','');
  window.Parsley.addValidator('profanitycheck', {
  validateString: function(value) 
    {
      var profanity_array = new Array("gosh", "darn", "dagnabit");
      var regex = new RegExp('\\b(' + profanity_array.join('|') + ')\\b', 'i' );
      return (!regex.test(value)); 
    },
    messages: 
    {
      en: 'Field includes profanity, please resolve.'
    }
  });
  
  //Disable the Submit button if the field with the profanityParsleyCheck class
  //changes and any field has parsley errors.
  $('.profanityParsleyCheck input').change(hideSubmitOnParsley);
  $('.profanityParsleyCheck textarea').change(hideSubmitOnParsley);
  $('.profanityParsleyCheck select').change(hideSubmitOnParsley);
  function hideSubmitOnParsley() {
    var noErrors = true;
    $(this).parsley().validate();
    $('.parsley-error').each(function() {
      noErrors = false;
    });
    if(noErrors) {
      $('.Submit').attr('disabled', false).css('opacity', '1');
    }
    else {
      $('.Submit').attr('disabled', true).css('opacity', '0.5');
    }
  }
  
});

 

Result:

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

Sign in to reply to this post.