Question T14058
Visible to All Users

Force utf-8 encoding for question values and upload file names

created a year ago

Hi,
we are currently starting our first survey and some results are not readable for us. I found the problem where the content of the JSON has to be utf-8 encoded due to our Backend. So there some results that are containing greek or spanisch characters. It there any build-in way to force utf-8 input? We got some problems with characters like ê or í.
We encountered the problem when we wanted to parse the response from our server into JSON to use the results in the Dashboard, but there were several illegal characters.

On one hand we thought about a solution using survey.onValidateQuestion to check if a question contains a non-latin character. Do you know some regex to check for that?
On the other hand a participant uploaded a file that has a file name which contains probably ANSI/Windows-1252 encoded characters. Is there a way to prevent that?

Thanks for your help!

Answers approved by surveyjs Support

created a year ago

Hello Martin,
Thank you for reaching out to us.

If you wish to validate a question value and check whether an answer contains non-latin characters, you can subscribe to the survey.onValidateQuestion event and validate options.value.

JavaScript
function containsNonLatinCharacters(text) { // Regular expression to match any non-Latin characters var nonLatinRegex = /[^\u0000-\u007F]/; return nonLatinRegex.test(text); } survey.onValidateQuestion.add((sender, options) => { if(containsNonLatinCharacters(options.value)) { options.error = "Ensure Latin characters only."; } });

View Demo

Regarding uploaded files: you can use the survey.onUploadFiles to verify uploaded files and ensure that they don't contain disallowed characters.

I hope this information helps. Please let me know if you have additional questions.