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?
Question
Question
Is it possible to eliminate commas being entered into the Other choice field
Answer
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:
Replies
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 :)
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.
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.
Alternatively: you could use workflow, using the the token calculator to remove the ,'s automatically before generating your CSV:
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.
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:
thank you that worked perfectly!
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.
Have you tried it with the back-end validation turned off for the form?