05/01/2012

Quick tip: Restricting text in a TextField

I was working on a issue where a user could enter any character in a  TextInput component, which I didn't want. I wanted them to only be able to enter text and spaces, no numbers or special characters.

Originally I thought about using a Validator to check the value entered in the TextInput component and display a error message if the user had entered a invalid character. Or use the change() event handler to check as the user types to see that the user is not entering a invalid character.

Searching around I found that there is a even more simple solution. In the TextInput component you can 'restrict' the characters that can be entered, like this:

myTextInput.restrict="A-Z\\a-z\\' '"

This restricts the characters that can be entered to A-Z or a-z or a space. The double back slashes are how you separate the sets of characters you want to restrict to.

Hope this helps someone.

No comments: