Ignition perspective Limit number of characters allowed in input text field

how to limit number of characters allowed in input text field and text area in ignition perspective

This might help.

This script is modified from what @pturmel wrote in the linked thread.

	tf = self
	if tf.custom.altMax > 1:
		if len(currentValue.value) > tf.custom.altMax:
			tf.props.text = tf.props.text[:tf.custom.altMax]
  1. Create the custom property altMax
  2. Turn off deferUpdates
  3. Turn off rejectUpdatesWhileFocused
  4. Put this in a property change script on the text property of text property on your text field.

That can even be simplified a bit. The full ChangeScript can just be one line:

self.props.text = self.props.text[:self.custom.altMax]

There’s no need for a length check.

TIL. I assumed slicing would throw an IndexOutOfBoundsException if self.custom.altMax was too big. Thanks for the simplification.

Thanks the simple solution. It work well

can you elaborate on how to limit my text box input to 17 character I am not sure where to find this on the manual for more explanation I would really appreciate it. FYI my tag is a string character

To follow the example that @zacht posted.

create a custom property on the component named altMax
set deferUpdates and rejectUpdatesWhileFocused to false

Righ click the text property, and then select Add Change Script...

Insert the script as @cmallonee posted (be sure to indent it correctly), click okay and you’re done.

I get a ‘NoneType’ object is unsubscriptable

Going to need more information to help. What does your code look like?

it had a binding that was messing it up

This solution works, but it relies on turning off deferred updates. I have a database form that saves each field as it is entered, and in this case I really need deferred updates to avoid sending every single keystroke to the database. It would be really useful to have access to the maxlength html property of the input!

I would recommend changing that. It is too easy to screw up your database. Consider arranging your GUI to highlight fields that are now different from the DB, and using a button to save them all together.