Limit the number of characters in input_text?

Is it possible to limit the number of characters while inputting a string into an input_text helper?

My scenario:

I’m storing a 4 digit lock pin/code in a input_text entity. I have the helper set for a max of four characters, but the when inputting it will still allow me to enter an unlimited number of characters (although it doesn’t accept the input if it’s over 4). I was wondering if it’s possible to make a hard stop to any additional character over 4 when inputting so I (or more likely someone else) tries to input more only to find out later the pin wasn’t accepted.

Does that make sense? Any suggestions?

So you want to truncate it at 4?

12345678
=>
1234
?

I assume you could use a second input text.
First one takes them all, then an automation takes the first four and stores them in the other.

Thanks for quick reply. That is correct. although I was hoping for a hard stop once I hit the four character limit. Instead it allows me to input a novel. I just figured the "max’ option feature would work that way. Guess not.

What is the input “source”?
If you create a picture elements card with 10 buttons then each button press could add one digit to the input text.
That way you could limit it.
But if the “source” is the entity itself and a keyboard then it’s probably not possible

My input source is straight from the keyboard. The picture elements card is an interesting way to attack the issue, but in this case, more effort than I want to put in for this project. Again, I appreciate the feedback!

Is it possible to use input_number instead?
If so, you can use min: 1000, max: 9999, step: 1

Min 1000 is not correct since a pin can be “0000”.
Pin codes are not numbers, they are string because of the leading zeros.

But with that said, 0 to 9999 and an automation to make it input text with leading zeros.

I forgot about the leading zero cases, thanks.

Haha…Well I actually thought about that…BUT here’s the rub. I really want the string to be hidden (i.e.password mode with the asterisks instead of visible) and I don’t see a way to do that with the number input. ugh! I can’t win!

There is a video that is probably one or two years old by Rob on the hookup channel where he creates an alarm panel in node red.
I have not used it myself but my memory says it looks like a picture elements card.

As far as I know, input_text doesn’t support that behavior.

All you can do is add a regex pattern like this:

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

It still allows you to type whatever you want and as much as you want but will only use what you entered if it contains four numbers (no more, no less). In the UI, the underline is red if the inputted text is invalid and only changes to blue when it’s 4 digits.

1 Like

Thanks @123 this is at least an improvement! Really appreciate all the input! (No phun intended…well maybe a little).

I have posted on a similar thread with a workaround:

It’s essentially this:

but you use only one input text and the automation edits it directly.