Select to view content in your preferred language

Numeric value needs to be entered with 3 decimals

1078
5
11-23-2023 01:36 AM
JelleTeunissen
New Contributor II

Hi All, I'm fairly new to Survey123 and running into a problem. In a survey, we have a field that needs to be entered by the user, using a specific format of 1 to 3 digits, and 3 decimals. Like this: 12.345 or 251.837 or 0.536. The 3 decimal places are mandatory, but the first 3 digits are optional. Also, we are in the Netherlands, so the decimal separator could be entered as a comma. Ideally, I would like to have the value converted to a value with a dot as decimal separator, or perhaps only accept the . and display a message when the entered value does not match the format of *0.000.

I have tried input mask, but that doesn't allow the first 3 digits to be optional. When used 9.999, it won't allow 12.345 and when used 999.999 as a mask, we need to enter 012.345.
I have also tried using a regex(., '^[0-9]{1,3}\.[0-9]{3}$'), but this way the 3 decimals are not mandatory and when a comma is used as the decimal separator, it does not work at all.

Is there any way to make this work?

I look forward to your replies, many thanks in advance!
Cheers, Jelle.

Tags (2)
0 Kudos
5 Replies
abureaux
MVP Regular Contributor

Using the Input Mask will force data into the format you need.

In the input_mask field, use: 000.999;#

9 is required. 0 is optional. In both cases, only 0 - 9 are accepted.

JelleTeunissen
New Contributor II

Aplogies for not replying sooner...

The mask won't allow less than three digits before the decimals, so 1,234 has to be 001,234. That's not going to work. I have tried using regex regex(.,'^[0-9]{1,3}[\,]\[0-9]{3}$') but that only responds to a dot (.) as a decimal indicator. We use (,) in the Netherlands.
If I use the mask 0,999;# it won't allow more than 1 digit before decimals.

Any ideas?

 

0 Kudos
abureaux
MVP Regular Contributor

I think this is the solution you are looking for:

abureaux_0-1706897298821.png

 

if(contains(${example1}, '.'),string-length(substr(${example1},string-length(if(round(${example1},0)>${example1}, round(${example1},0)-1,if(round(${example1},0)<${example1}, round(${example1},0)+1,round(${example1},0)))),string-length(${example1})))=4,'false')

 

 

Just replace example1 with your input field. And if you want to test for a different number of decimal places, change the 4. E.g., If you want to round to 5 decimal places instead, then 5+1=6, so replace 4 with 6.

Here is the breakdown:

abureaux_3-1706896223958.png

namelogic
a1Tests for a decimal
b1Isolates the value before the decimal place (only preserves number of digits - this value will not always equal your input, but that's fine)
b2Starting position for the next step.
b3Trims our input value. Starts at the decimal and ends at the end of the input value. In other words, this isolates everything after the decimal.
b4

tests to see if the remaining string is 4 (remember that a decimal place is still a character, so 4 characters = 3 decimal places)

 

I was originally going a different route, but then realized what you were more likely looking for. I'll leave this here since there is some good info on input masks at least.

Am I correct in assuming you are want users to enter the following combinations?

  • 000.000
  • 00.000
  • 0.000
  • .000

If so, the input mask of 000.999 will do that (feel free to swap the '.' for ','). If you need more optional characters before the decimal, just add more zeros (e.g.,  000000.999). As I mentioned, 0 is optional and 9 in mandatory. In both cases, they only accept 0 - 9.

The character placeholder is not required for input masks, but I tend to always include it as it shows the user what is expected of them, and also because Excel turns numbers like 000.999 into 0.999. Adding the character placeholder turns the cell into text automatically which reduces headaches: 000.999;#

Note that you are not locked into using '#' as the placeholder. The semicolon is mandatory, but you can put whatever you want after it (e.g., 000.999;_).

The problem with input masks is that when you click into a cell, you always enter text from the far left. So in the case of an input mask like 000.999, you aren't required to enter the first three digits, but it is very annoying to skip those digits. It requires a lot of effort on the part of the end user.

0 Kudos
JelleTeunissen
New Contributor II

The mask is not working, it looks like the 0 is not optional. I do want the values to be entered as 

  • 000,000
  • 00,000
  • 0,000

Using the mask 0,999 won't allow me to enter 12,345 , while using the mask 000,999 won't allow me to enter 1,234. Somehow, the '0' in the mask is not mandatory, it needs to have a value. So using the mask 000,999 forces me to enter 001,234

Any ideas? 

0 Kudos
JelleTeunissen
New Contributor II

.

0 Kudos