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
Accepted Solution

On button click or onFocus, check validation of textfield

I have 3 editfields and a button on my form.

Fields are:

Name (allows only char and space)

Phone (allows only number input)

Email (Validation has been applied to this)

 

I have a button SAVE.

 

Now before i can actually hit the save button,incase any of the field is empty it should throw a dialog error or any other prompt inorder to prevent blank data entry. In my email field i have inserted validations for all of the email properties including the case of leaving the email field blank.

 

But now what i want is, all the fields should be checked for empty or blank space at one go on button click or on focus.

 

Any one sound on this please guide,

 

Thanks.

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

Re: On button click or onFocus, check validation of textfield

The button click event should be handled by the FieldChangeListener.

 

For the onFocus, I guess you meant when the user tries to leave the field, so you should overwrite the onUnFocus method.

 

E.

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

Re: On button click or onFocus, check validation of textfield

How can i set field change listener in button to check for textfields.

 

This is my code snippet for button changed listener:

ButtonField showInputButton = new ButtonField("\n\n\n  Save   ",ButtonField.FIELD_HCENTER | ButtonField.CONSUME_CLICK);
          showInputButton.setChangeListener(new FieldChangeListener() 
          {
            
                public void fieldChanged(Field field,int context) 
                {
                   }
            }

 How to access for other fields? The only validation i am interested in performing on this button click is checking if the other fields are empty.Please guide.

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

Re: On button click or onFocus, check validation of textfield

[ Edited ]

FieldChangeListener is an Interface. Instead of implementing it anonymously, let your screen implement it and check which field is passed as a parameter:

 

public void fieldChanged(Field field,int context)
{
if(field == saveButton)
{
if(textField1.getText() == null || textField1.getText().trim().length() == 0)
{
Dialog.alert("Message to user");
}
}
}

 

 

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

Re: On button click or onFocus, check validation of textfield

[ Edited ]

Hi

 

I tried just like u suggested. I included the below code within my button manager and it worked great!!! Thanks a lot

 

public void fieldChanged(Field field,int context)
            {
                if(field == showInputButton)
                {
                    if(TextField1.getText() == null || TextField1.getText().trim().length() == 0 || TextField2.getText() == null || TextField2.getText().trim().length() == 0 || TextField3.getText() == null || TextField3.getText().trim().length() == 0)
                        {
                            Dialog.alert("Cannot leave field blank!");
                        } 
                        
                }
            }