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

Editfield listener to check length of field

[ Edited ]

I have a phone field which is a editfield.

I have been able to restrict input to 10 digits in this field to hold mobile number values.

 

But now i want to add an additional validate that throws a dialog error once the user has typed a value lesser than 10 in this field.

 

This action must be triggered as soon as the user has typed something and gone to next field.

 

Heres my code:

LabelField lbl1 = new LabelField(" Phone No:            ");
       
       final EditField TextField2 = new EditField("", "", 10, 0)
       {
          //Some statements
       }
TextField2.setChangeListener(new FieldChangeListener() 
        {
          public void fieldChanged(Field field, int context) 
          {
              if(TextField2.getText().length() < 10)
                          System.out.println("Please enter a valid 10 digit mobile number!");
          }
        });

 

 Please help. Thanks.

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

Re: Editfield listener to check length of field

As @simon_hain once said, allow me to quote myself: "you should override the onUnFocus method"

 

protected void onUnfocus()
{
if(getText() == null || getText().length() < 10)
Dialog.alert("Text too short!");
}
Contributor
Posts: 25
Registered: ‎05-25-2012
My Device: 9000 Series
My Carrier: Vodafone

Re: Editfield listener to check length of field

Thanks so much @maadani.
Contributor
Posts: 25
Registered: ‎05-25-2012
My Device: 9000 Series
My Carrier: Vodafone

Re: Editfield listener to check length of field

[ Edited ]

I have one more doubt. @maadani

 

How can i prevent user from inserting a space at start of field for Name editfield

 

protected boolean keyChar(char ch, int status, int time) 
                {
                if (CharacterUtilities.isLetter(ch) || (ch == Characters.BACKSPACE || (ch == Characters.SPACE))) 
                {
                return super.keyChar(ch, status, time);
                }
               return true;
                }

 

 

Though i would want to allow the user to insert space in between to allow full names.

 

Would appreciate a help.

 

Thanks.