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
Contributor
Posts: 25
Registered: ‎05-25-2012
My Device: 9000 Series
My Carrier: Vodafone
Accepted Solution

Impose fixed length validation

I have a editfield to contain PHONE NO. input.

I would like to have a fixed length entry of 10 digits (not more or less)

 

How can i achieve this?

 

This is my code for phone editfield:

//Add box next to field for containing input 
       HorizontalFieldManager hfm1 = new HorizontalFieldManager();
       LabelField lbl1 = new LabelField(" Phone No:            ",10);
//Adding 10 here doesnt help
       
       final EditField TextField2 = new EditField()
       {
           boolean _drawFocus = false;
            protected void layout(int maxWidth, int maxHeight) 
            {
                super.layout(Math.min(maxWidth, 300), Math.min(maxHeight, 20));
            }
          protected boolean keyChar(char ch, int status, int time) 
                {
                if (CharacterUtilities.isDigit(ch) || (ch == Characters.BACKSPACE)) 
                {
                return super.keyChar(ch, status, time);
                }
               return true;
                }
}
 };
       TextField2.setBorder(BorderFactory.createRoundedBorder(new XYEdges(2,2,2,2)));
       hfm1.add(lbl1);
       hfm1.add(TextField2);
       add(hfm1);  

 Please guide.Thanks

Developer
Posts: 729
Registered: ‎05-04-2011
My Device: 9700

Re: Impose fixed length validation

Hi,

 

Use the fieldChangeListener and check the length of text when the listener is called.

 

Check out this thread:

http://supportforums.blackberry.com/t5/Java-Development/EditField-listener-for-typing/td-p/432271

 

E.

Contributor
Posts: 25
Registered: ‎05-25-2012
My Device: 9000 Series
My Carrier: Vodafone

Re: Impose fixed length validation

Thanks maadani. But i want to actually prevent the user from typing more than 10 digits, how can go about that. The link sure can be implemented to throw prompts on change listener.
Developer
Posts: 729
Registered: ‎05-04-2011
My Device: 9700

Re: Impose fixed length validation

Use this const.:

public EditField(String label,
                 String initialValue,
                 int maxNumChars,
                 long style)

http://www.blackberry.com/developers/docs/5.0.0api/net/rim/device/api/ui/component/EditField.html#Ed...

 

E.

Contributor
Posts: 25
Registered: ‎05-25-2012
My Device: 9000 Series
My Carrier: Vodafone

Re: Impose fixed length validation

As u may have seen in my code i have define my label and editfield seperately. And i am using this to define editfield
final EditField TextField2 = new EditField()
{
}
Can i use it this way
final EditField TextField2 = new EditField(lbl1,10)
{
}
Also i do not wish to suplly an initial value.
Req. u to please refer my code i have enclosed in my question.
Developer
Posts: 729
Registered: ‎05-04-2011
My Device: 9700

Re: Impose fixed length validation

If you don't want initial value, you can use it like this:

 

final EditField TextField2 = new EditField(yourLabel, "", 10, 0); //0 means no style.

And, if you wish to use your label separately:

 

final EditField TextField2 = new EditField("", "", 10, 0); //0 means no style.

E.

Highlighted
Contributor
Posts: 25
Registered: ‎05-25-2012
My Device: 9000 Series
My Carrier: Vodafone

Re: Impose fixed length validation

Thank you so much.It worked!!

Request you to look into this issue of mine as well(its kinda related to this)

http://supportforums.blackberry.com/t5/Java-Development/On-button-click-or-onFocus-check-validation-...