How to automatically insert commas into text entry box #3374
-
I'm using jsPsychSurvey to ask people to estimate the population of various countries. I would like commas to be automatically inserted into their answers as they type to help them read 2,000,000 vs 20,000,000 for example. Since jsPsychSurvey is a wrapper for SurveyJS, I looked at how to do this in SurveyJS and found https://surveyjs.io/form-library/examples/masked-input-fields but I don't have a js framework set up for my experiment (which this masking appears to require) and I don't know what would be best practice around this in jsPsych. All advice would be appreciated! (For context, I have some experience with jsPsych but only the bare minimum with js frameworks.) Here's the timeline element as an example:
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
Hi @noa-no-h I haven't tried this yet, but the masked input field might work as is? This is the relevant bit of the demo from the surveyjs site. What happens if you add the maskType field to your json description? {
"type": "text",
"name": "decimal",
"title": "Decimal:",
"maskType": "numeric",
"maskSettings": {
"precision": 1
}
}, |
Beta Was this translation helpful? Give feedback.
-
Thank you, @jodeleeuw ! (here's the code:)
|
Beta Was this translation helpful? Give feedback.
Great question @noa-no-h, and sorry that this isn't clear from the
survey
plugin docs/examples.Josh is right that you can use the SurveyJS masked input to do this. Using
maskType: "numeric"
automatically gives you the commas for the thousands separator. And adding"precision": 0
in themaskSettings
prevents people from entering decimals. See here for more info and examples for the masked input: https://surveyjs.io/form-library/examples/masked-input-fields/documentation.The reason why Josh's suggestion isn't working is that you have to remove the
inputType: "number"
parameter because that is what is producing the weird behavior you're seeing (it causes an error in the SurveyJS core packa…