Looking for a way to implement a regex on the input_text with an option to randomize the numbers

Hey !

I’m using the input_text to set up a door lock using a custom publish via MQTT
looking for two things and could not find anything online that worked on my setup.

  1. A way to limit the number of characters and only allow numbers in the input text (it’s possible to setup a maximum number of digits in the input_text, but it allows the user to type whatever it wants, and will not update the entity, I was looking for a way to limit the input in the input procedure)

  2. a way to generate a random password with 6 digits (only numbers)

Can’t be done. Putting a regex validator on an input_text helper only does one thing, and that is to highlight the field in red (in the UI/on the dashboard) if the regex does not match. It will not prevent any value from being stored/submitted.

The only way I can see to prevent disallowed values would be an automation with a state trigger bound to your input_text. If a disallowed value is entered, immediately switch it back to the previous hopefully known good value.

Is there any special thing to do to get the regex working? I couldn’t see the red you mentioned

I didn’t need to do anything special. What is your regex and from where are you editing the contents of the text_input? It may not be visible on say all kinds of dashboard cards. But if you edit the value directly from the helper info page, the one you get if you go into Settings > Devices & services > Helpers > your text_input it should work. Or at least it does for me.

This is my regex:

^[0-9]{6,6}$

I see the red when editing, but still non-numeric characters are accepted and it’s stored in the entity

Again, that is the only thing regex validation does. It cannot/will not prevent any value from being stored.

Got it,
So I’ll try to create an automation to discard the change (set the previous value) if the regex is not followed.
Thanks