# Limit Characters in a Textbox when Typing

**URL:** <https://forum.inductiveautomation.com/t/limit-characters-in-a-textbox-when-typing/29372>\
**Category:** Ignition\
**Tags:** ignition79, vision\
**Created:** [October 24, 2019, 6:17pm UTC](https://forum.inductiveautomation.com/t/limit-characters-in-a-textbox-when-typing/29372 "2019-10-24T18:17:30Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![boyergary](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/boyergary/32/10316_2.png) [@boyergary](https://forum.inductiveautomation.com/u/boyergary)\
**Post date:** [October 24, 2019, 6:17pm UTC](https://forum.inductiveautomation.com/t/limit-characters-in-a-textbox-when-typing/29372/1 "2019-10-24T18:17:30Z")

</div>

I simply want to set a maximum length on a textbox so that when the user gets to that point, they cannot type any more. Evidently, the Maximum Characters property doesn't actually prevent someone from continuing to type (which is misleading in the documentation).

I tried to capture keystrokes and keep count and remove the last character if they've exceeded the limit, but then I have to reset focus to the end of the typed characters because the focus resets when I update the field. And if I turn off Reject Updates During Edit, I can't update the field at all while I'm typing.

I can't believe this very simple UI function is missing, so please tell me that I'm wrong!

Thanks!

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [October 24, 2019, 7:04pm UTC](https://forum.inductiveautomation.com/t/limit-characters-in-a-textbox-when-typing/29372/2 "2019-10-24T19:04:39Z")

</div>

Add an integer custom property to the field to hold your length limit (doesn’t work with the built-in). I used `altMax` for the following:

```auto
if event.propertyName == 'text':
	tf = event.source
	print event
	if tf.focusOwner and tf.altMax > 1:
		if len(event.newValue) >= tf.altMax:
			tf.select(tf.altMax-1, len(event.newValue))
			if len(event.newValue) > tf.altMax:
				tf.replaceSelection(event.newValue[tf.altMax-1])

```

Turn off defer updates and reject updates.

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [October 24, 2019, 7:14pm UTC](https://forum.inductiveautomation.com/t/limit-characters-in-a-textbox-when-typing/29372/3 "2019-10-24T19:14:36Z")

</div>

Simple with a regex pattern. Use a Formatted Text Field and use

```auto
^[\pL\pN]{0,5}+$

```

for the regex pattern. Then uncheck the `Allows Invalid Text` property.This regex will allow any alphanumeric and limit the input between 0 and 5 characters long. Change as needed.

---

<div class="post-metadata">

**Author:** ![boyergary](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/boyergary/32/10316_2.png) [@boyergary](https://forum.inductiveautomation.com/u/boyergary)\
**Post date:** [October 24, 2019, 7:38pm UTC](https://forum.inductiveautomation.com/t/limit-characters-in-a-textbox-when-typing/29372/4 "2019-10-24T19:38:26Z")

</div>

> [@dkhayes117](#):
>
> \[1\]{0,5}+$

Thank you both. That second solution is very succinct and exactly what I needed.

* * *

1. \pL\pN

---

<div class="post-metadata">

**Author:** ![Rusty](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/rusty/32/19859_2.png) [@Rusty](https://forum.inductiveautomation.com/u/Rusty)\
**Post date:** [August 13, 2020, 7:09pm UTC](https://forum.inductiveautomation.com/t/limit-characters-in-a-textbox-when-typing/29372/5 "2020-08-13T19:09:02Z")

</div>

> [@pturmel](#):
>
> if event.propertyName == 'text': tf = event.source print event if tf.focusOwner and tf.altMax \> 1: if len(event.newValue) \>= tf.altMax: tf.select(tf.altMax-1, len(event.newValue)) if len(event.newValue) \> tf.altMax: tf.replaceSelection(event.newValue[tf.altMax-1])

This worked great for me for a Vision Text Area, thanks!

---

<div class="post-metadata">

**Author:** ![dario.cappuccio](https://avatars.discourse-cdn.com/v4/letter/d/b19c9b/32.png) [@dario.cappuccio](https://forum.inductiveautomation.com/u/dario.cappuccio)\
**Post date:** [April 2, 2025, 7:46am UTC](https://forum.inductiveautomation.com/t/limit-characters-in-a-textbox-when-typing/29372/6 "2025-04-02T07:46:48Z")

</div>

hi,  
I have to do the same thing in perspective. Any suggests? Thanks in advance

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [April 2, 2025, 8:21am UTC](https://forum.inductiveautomation.com/t/limit-characters-in-a-textbox-when-typing/29372/7 "2025-04-02T08:21:42Z")

</div>

This thread is for Ver. 7.9 and so is Vision only. Please don't mix Vision and Perspective questions in the one thread as it causes confusion.

You can start a new thread if [Ignition perspective Limit number of characters allowed in input text field](https://forum.inductiveautomation.com/t/ignition-perspective-limit-number-of-characters-allowed-in-input-text-field/37937) doesn't answer your question.
