check number (parse to string?)

I'm trying to create a small program for teaching students how to do basic accounting. They have some financial documents they have to code first before they can implement it online. I want to check whether the know how to code those documents. An example might be: 0200 buildings € 200.000 0510 mortgage € 200.000 I need a check for the numbers in the frontline. Those numbers have to exist of exactly 4 numbers and can contain basically any number from 0 to 9999. The problem is that the number also has to be exact 4 digits. If a number is smaller than 4 digits, zero's have to be added as prefix. A person should be able to store such number in the database (at the moment it's done through a textbox and I'm trying to check it through a microflow) and another person has to be able to retrieve that exact number from the database too (most likely through a reference selector). I first made the number an integer and I could check if the number was 0<number<10000 but the problem was that I couldn't store the prefix 0. Now I was thinking to store it as an integer, but how can I check whether the input is really a number and how can I check if that number is between 0 and 10000?   Anyone any suggestion how I could try to solve this? (I'm still fairly new to coding and Mendix and trying to learn Mendix, so newbie language would be awesome)
2 answers

You could use a regex for that. If you want to find any series of 4 digits in a string /\d\d\d\d/ or /\d{4}/ will do. But indeed it means using a string of 4 characters. If it fails the regex show a message to the user what valid strings are.

If it is valid you could parse it also to an integer and store both values in two different attributes (as a string and as an integer). Gives you some more flexibility if you need either one of the values.





Store this value as a String with 4 chars

When you commit the value using the save button, use microflow instead of save to do the extra validations:

1) create a variable to parse the value to an integer using parseInteger. More info here: When the input cannot be parsed, provide validation feedback to inform the user. 

2) after parsing, you can check the parsed number provided is between 0-9999. If not, again provide validation feedback. 

3) use the length function to determine the length of the string. If the length is lower than 4, add zero's in front using string concatenation. (if $length = 3 then '000' + $StringInput else if $length = 2 then '00` + $StringInput else if $length = 1 then '000' + $StringInput else $StringInput

(there a nicer ways to do this, but because this is a fixed length, you're fine with this logic) 

4) Commit Object

5) Use a ref selector and point to the string attribute