Field entry limited to a specific alpha-numeric format

I want to only allow entries into a specific field that follow this specific format:
One alpha character, followed by 4 numeric characters. If anyone tries to enter a value in the field that does not conform to this format - an error alert should be displayed. Can someone please let me know if it is possible to limit a field entry this way?

Thank you!

Try the following regex: ^[A-Za-z]\d{4}$

Thank you @adamhholmes , I appreciate the reply. I’m still a novice with Bubble - would you be able to tell me where to find the place to enter that regex code?

Sure…

Here’s how I would do it… (this is just an example… how you implement it in your own app depends on exactly what you want to achieve with your UX).

First, I’d add a group to act as a variable (called ‘Input Invalid?’) to determine whether the input is invalid.

This group would have a content type of ‘yes/no’

Then you need to establish whether the input is valid or not, and assign a value to that group based on that (i.e. if the input is invalid, set the value to Yes).

To do that, you need to do the folowing:

Fist set the max characters of the input to 5
Get the input’s value
Extract with regex ^[A-Za-z]\d{4}$

That will extract any texts from the input value that match your specific format, into a list of text.

So then all you have to do, is check if that list has any values or not, by using :first item is empty

If it is empty, then nothing matches and the input is invalid.


So, to do all of that, just set the datasource of your Input Invalid? group to: input's value: extract with regex: first item is empty (using the regex above as the regex pattern).

The issue now is that the value of the Input Invalid? group will be set to yes even before the user has finished typing, so to correct that, we only want to set the value to Yes when the input value is the wrong format AND contains 5 characters… so we can add that to the expression in the Is Invalid group’s datasource.

So the full expression will now look like this:

input's value: extract with regex: first item is empty AND input's value's character count is 5

Now you just need to make something happen when the Input Invalid?'s yes/no evaluates to Yes… i.e. show an error text.

So, add a text element, hidden on page load, and simply put a condition to make it visible when Group Input Invalid?'s yes/no is Yes

There’s still one more thing to address - and that is, if we leave it at that, then the error text will only display once the user has entered 5 characters (meaning if they enter “abc” and then click away from the input element, the error message will not display).

To fix that we can add a couple more things to the condition on the text element, so that the full conditional expression to display the text will be:

Group Input Valid?'s yes/no is Yes or (input isn't focussed and ((input's value's number of characters < 5) and input's value is not empty)

note: you’ll need to have enabled the experimental parenthesis feature in your app for this to work

If you want to display a similar message to confirm when the input value is valid, then I’d set a separate variable for that, using the same method outlined above, to show the same text element with different text, based on another condition.

It sound’s fairly complicated, but it’s not too hard to do.

Here’s a working example you can look at:

Input Validation (bubbleapps.io)

2 Likes

Whew! Thank you very much for the detailed reply. This, along with your working example helped me out. I’ve set it up the way you described, and something is still not working right. I believe it has to do with the expression parentheses. When I create the conditionals, they end up looking like this -

((Group Input Invalid’s yes / no is “yes” or Input [InputName] isn’t focused) and (Input [InputName]'s value:number of characters < 5)) and Input [InputName]'s value is not empty

This follows the same sequence of expressions in your working example, but the system is automatically placing these parentheses in places that do not align with your example. I tried manually force-typing the parentheses where I want, but either Bubble is not letting me, or I am doing it wrong. I will do some more research and keep trying!

It an be a bit fiddly to understand how the parenthesis work at first… it’s all about where you click, and which ‘click’ button you click on… but once you understand it it’s not too bad.

To close the loop—IT WORKS!
I had to get creative and type in the conditional in something of a reverse order because of how the parentheses were forcing themselves in. What I ended up with is –

(((Input [InputName]'s value:number of characters < 5) and Input [InputName]'s value is not empty) and Input [InputName] isn’t focused) or Group Input Invalid’s yes / no is “yes”

THAT did the trick. Thanks again for your assistance @adamhholmes

1 Like

Glad you got it working :slight_smile:

1 Like