Limit Characters in a Textbox when Typing

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:

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.

1 Like