Text field format restrictions and conditions

newbie-question

#1

Hello,

I am looking to restrict what the user can input into a text field based on specific format of alpha/numeric characters. I would also like to introduce hyphens as separators.

Field format: NNNN-AA-NNNNN

I require the input to be 4 numeric characters, followed by 2 alpha characters, and another 5 numbers.
I would like to prevent the user from entering a numeric character if alpha is expected, and vice versa. I would also like to auto-populate the hyphens when the user enters the 4th and 6th characters (and for them to be auto-deleted when the users backspaces).

Any help would be appreciated!


#2

I recommend using the Text Field’s Key Up event, which is fired each time a keyboard key is released. You might be tempted to use Text Changed but you will be programmatically changing the text value when insert dashes, and likely when you restrict input (e.g., if user tries to enter ‘A’ first, you’ll want to delete that char.) Depending on what kind of precise control you want, you could use or combine use of the Key Down event.

You’ll need to set up some conditional cases to handle each of your constraints.

Psuedo-code looks like this:
//where “This” refers to the widget–your text field
// [[This.slice(-1)]] is an expression that returns the last char of the text value in the text field
// [[This.slice(0, -1)]] is an expression that strips the last char of the text value

Key Up
Case 1: auto-delete dashes
If [[This.slice(-1)]] equals “–”
Set value of This to [[This.slice(0, -1)]]

Case 2: ensure first 4 chars are numeric
Else if length of This is less than or equal to 4 and value of [[This.slice(-1)]] is not numeric
Set value of This to [[This.slice(0, -1)]]

Case 3: insert dash after 4th char
Else if length of This is equal to 4
Set value of This to [[This]]–

Case 4: ensure alpha for next 2 chars
Else if length of This is less than or equal to 7 and value of [[This.slice(-1)]] is not alphabetical
Set value of This to [[This.slice(0, -1)]]

Case 5: insert dash after 7th char (4+dash+2)
Else if length of This is equal to 7
Set value of This to [[This]]–

Case 6: ensure next 5 chars are numeric
Else if length of This is less than or equal to 13 and value of [[This.slice(-1)]] is not numeric
Set value of This to [[This.slice(0, -1)]]

Case 7: ensure only 13 chars (4+dash+2+dash+5)
Else if length of This is greater than 13
Set value of This to [[This.slice(0, -1)]]

As I recall, “numeric” logically includes periods and maybe commas. If you need to enforce true digits, I think you have to use the “is one of” option in the condition builder and manually type in each digit as a set.

If this doesn’t make sense, you don’t have experience creating conditional logic, or whatever, reply and I’ll try to build you a demo. Or, if you get close but something not working right, you can post your attempt here and I (or other forum folk) can likely fix it. Best of luck!