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.
Regards,
Ronald
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: https://docs.mendix.com/refguide/parse-integer. 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