Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Posts: 46
Registered: ‎07-12-2008
My Device: Not Specified

Input Text Validation

I've got a BasicEditField that uses a custom TextFilter to display some data that gets downloaded from a server.  There is a possibility that the data from the server has bad characters on it.


When I call setText() on the BasicEditField, I get a "java.lang.IllegalArgumentException: invalid text" exception thrown.  This makes sense because the text I'm passing to setText has bad characters.


When I create my custom TextFilter, I have to override both the convert and validate methods.  Stepping through the code I never see the convert method being called when calling BasicEditField.setText().  How/when is the convert method called?  From the javadocs it appears that it should filter out bad characters.


I think I've found a workaround to accomplish what I want by temporarily setting the text filter to null, setting the text, and then applying the filter again.

// field is a BasicEditField // tmp is a String TextFilter old = field.getFilter(); field.setFilter(null); field.setText(tmp); field.setFilter(old);


Here is my custom TextFilter class:


class SimpleTextFilter extends TextFilter { private static String VALID_CHARS = "~!@#$%*()-+=;:,.? "; public char convert(char character, int status) { if (!validate(character)) return 0; return character; } public boolean validate(char character) { if (character >= 'a' && character <= 'z') return true; if (character >= 'A' && character <= 'Z') return true; if (character >= '0' && character <= '9') return true; return (VALID_CHARS.indexOf(character) != -1); } }


Thanks for any help!

BlackBerry Development Advisor
Posts: 15,871
Registered: ‎07-09-2008
My Device: BlackBerry PRIV
My Carrier: Bell

Re: Input Text Validation

This is the expected behavior.  TextFilters are designed to filter a user's input, not programmatic input.  You should validate the input from the server before putting it in the BasicEditField.

Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.