Welcome!

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

Reply
Developer
Posts: 154
Registered: ‎01-08-2010
My Device: 8310 Rogers
My Carrier: Vodafone
Accepted Solution

How to allow user to enter only alpha numeric characters in text field?

Hi

 

Is it possible to restrict user to enter only alpha numeric characters in the textfiled?

 

 

Thanks

Developer
Posts: 587
Registered: ‎01-19-2010
My Device: BOLD 9700 OS5.0.0.x on Rogers Canada
My Carrier: Rogers

Re: How to allow user to enter only alpha numeric characters in text field?

You could check each character as it's entered, and remove that character if it isn't alpha numeric, but this process isn't overly efficient. I don't know of another way, but I'm sure it's out there.

----------------------------------------------------------------------------
chown -R us ./base
~J!NX
Developer
Posts: 154
Registered: ‎01-08-2010
My Device: 8310 Rogers
My Carrier: Vodafone

Re: How to allow user to enter only alpha numeric characters in text field?

Basically, pushing the enter key while in the textbox creates a newline. So, we need to prevent this so that only alphanumeric characters are valid in this field and nothing else should be accepted, including CRLF.

Developer
Developer
Posts: 1,123
Registered: ‎02-10-2009
My Device: 8130 / 8350 / 9530 / 9550 / 9850 / PlayBook
My Carrier: Verizon

Re: How to allow user to enter only alpha numeric characters in text field?

you could check only for the characters you want to filter (include or exclude) and pass the rest to super.keydown() / keychar(), etc

Developer
Posts: 587
Registered: ‎01-19-2010
My Device: BOLD 9700 OS5.0.0.x on Rogers Canada
My Carrier: Rogers

Re: How to allow user to enter only alpha numeric characters in text field?

You could always override the enter key to exit the field or proceed to the next screen. I'm assuming however that you want it to be a little more comprehensive than that. Browsing the Javadocs I can't really see any native methods to handle what you want.

 

Sorry,

~Dom

----------------------------------------------------------------------------
chown -R us ./base
~J!NX
Developer
Posts: 154
Registered: ‎01-08-2010
My Device: 8310 Rogers
My Carrier: Vodafone

Re: How to allow user to enter only alpha numeric characters in text field?

We can't allow user to hit "enter" button and then check for the CRLF ourself in code. Because  when user hit "enter" button a newline is cretaed in the textfield on the screen.

 

Can we prevent this(creation of newline on screen only rather that filtering it in code)?

 

 

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: How to allow user to enter only alpha numeric characters in text field?

For BasicEditFields and derived classes, you can create the Field using

NO_NEWLINE

which provides this functionality. 

Developer
Posts: 154
Registered: ‎01-08-2010
My Device: 8310 Rogers
My Carrier: Vodafone

Re: How to allow user to enter only alpha numeric characters in text field?

Hi

 

I could prevent user from entering newline using NO_NEWLINE.  But is there any way i could prevent them from entering charactyers other than alpha numeric.

 

Thanks

Developer
Developer
Posts: 1,123
Registered: ‎02-10-2009
My Device: 8130 / 8350 / 9530 / 9550 / 9850 / PlayBook
My Carrier: Verizon

Re: How to allow user to enter only alpha numeric characters in text field?

could you not use an editfield and use the filter masks?

Developer
Posts: 2,268
Registered: ‎07-08-2009
My Device: various
My Carrier: various

Re: How to allow user to enter only alpha numeric characters in text field?

[ Edited ]

As of v4.7, I don't see any useful filter suitable for your purposes.  So why don't you override keyChar() like this:

 

EditField eField = new EditField("Enter letters and digits only: ", "") {
    protected boolean keyChar(char ch, int status, int time) {
        if (CharacterUtilities.isLetter(ch) || CharacterUtilities.isDigit(ch) || (ch == Characters.BACKSPACE)) {
            return super.keyChar(ch, status, time);
        }
        return true;
    }
};

I haven't tested this code yet, but you can give it a shot...

You might want to add some handling (or super.keyChar(...)) for Characters.ENTER and Characters.ESCAPE, as well as anything else you find useful, such as comma and dot.

 

Hope this helps,

    Arkady.

Edit: added Characters.BACKSPACE - you do want to be able to edit it, don't you?

 

----------------------------------------------------------
please click 'Accept Solution' on posts that provide the solution to the question you've posted. Don't say "Thanks", press 'Like' button instead!