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

Question

Question

Is it possible to eliminate commas being entered into the Other choice field

asked on February 21, 2023

We have a form with several "Other" Choice fields.  However, in order to send a csv report through workflow we dont want the user to be able to enter in a comma into their response.  Since you cant use a regular expression is there anyway to prevent this using JS?  

0 0

Answer

SELECTED ANSWER
replied on February 24, 2023

Here's the code as promised.

On your form, add the noCommasValidation class to the field(s) you want to validate.  Then add this Javascript: 

$(document).ready(function () {
  
  //Add parsley validation to field(s) with the noCommasInOther class.
  //If the values of those fields have commas, they will fail the validation.
  $('.noCommasValidation input').attr('data-parsley-nocommas','');
  $('.noCommasValidation textarea').attr('data-parsley-nocommas','');
  window.Parsley.addValidator('nocommas', {
    validateString: function(value) {
      console.log(value.indexOf(','));
      return (value.indexOf(',') < 0);
    },
    messages: {
      en: 'Please do not include commas in this field.'
    }
  });
  
});

 

This is tested on the Classic designer on Forms Version 11.0.2212.30907.

Here's how it looks:

0 0

Replies

replied on February 21, 2023 Show version history

For JavaScript, it will depend on which Form designer you are using, Modern or Classic. With the Classic Designer it would be possible to remove the commas via JavaScript.

 

Happy to help further :)

 

 

0 0
replied on February 22, 2023

Yes for this one I am using Classic as it has a bit of JS in it and it doesnt seem the Modern has enough functionality yet to those codes.  Any help you can give would be great!

0 0
replied on February 22, 2023

Do you have any control over how you write data to CSV? CSV files can handle commas in the actual data if the values are enclosed in double quotes.

0 0
replied on February 22, 2023

That is true, but these are automated reports with about 50-60 free form fields, it would be hard to scan it through to look for the comma's and put the quotes around them.  I would rather just eliminate them altogether from being used.  I can use regex on the fields but multi-fields and Other choices dont have the option.

0 0
replied on February 27, 2023

Alternatively: you could use workflow, using the the token calculator to remove the ,'s automatically before generating your CSV:

1 0
replied on February 24, 2023

If you are on the classic designer you could use Javascript to add a custom parsley validator to the field.

I’ll try really hard to come back to this post later today with some sample code.

0 0
SELECTED ANSWER
replied on February 24, 2023

Here's the code as promised.

On your form, add the noCommasValidation class to the field(s) you want to validate.  Then add this Javascript: 

$(document).ready(function () {
  
  //Add parsley validation to field(s) with the noCommasInOther class.
  //If the values of those fields have commas, they will fail the validation.
  $('.noCommasValidation input').attr('data-parsley-nocommas','');
  $('.noCommasValidation textarea').attr('data-parsley-nocommas','');
  window.Parsley.addValidator('nocommas', {
    validateString: function(value) {
      console.log(value.indexOf(','));
      return (value.indexOf(',') < 0);
    },
    messages: {
      en: 'Please do not include commas in this field.'
    }
  });
  
});

 

This is tested on the Classic designer on Forms Version 11.0.2212.30907.

Here's how it looks:

0 0
replied on February 26, 2023

thank you that worked perfectly!

1 0
replied on March 21, 2023

It appears this script has an issue with the User Validation.  I set it up and it did work as you showed in your example, however, if fields in the form were not filled in but required it didn't kick the user back to the required field, it gave a backend validation error and required you to use the Recover Form button and go back.  It would just list the codes for all the fields that were not filled out but required.

0 0
replied on March 21, 2023

Have you tried it with the back-end validation turned off for the form?

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

Sign in to reply to this post.