views:

31

answers:

1

Hi friends,

Please send me sample code for Validation for Percentage Edit field in blackberry. Like it will accept 1 to 100 and also 00.00 format

thanq

A: 
final EditField editField = new EditField(EditField.FILTER_REAL_NUMERIC);
        editField.setFilter(new TextFilter(){
            public char convert(char c, int status) {
                if (!validate(c))
                    return 0;
                return c;
            }

            public boolean validate(char c) {
                boolean isValid = false;
                try {
                    String text = editField.getText();
                    double per = Double.parseDouble(text + c);
                    if (text.length()<=4&&per < 100 && per >= 0) {
                        isValid = true;
                    }
                } catch (NumberFormatException e) {
                    isValid = false;
                }

                return isValid;
            }

        });
Vivart
Hi Vivart i need percentage validation like it enter 1 to 100 and also 00.00 format
upendra
see my edited post.
Vivart
Hi Vivert thanq very much for sending this code.But i am getting one another issue when we are using this code. For entering numbers always need to press on alt button, so without pressing alt button need to enter the numbers for that how can we write code
upendra
The right way is to use Filter.http://www.blackberry.com/developers/docs/4.0.2api/net/rim/device/api/ui/component/BasicEditField.html#setFilter(net.rim.device.api.ui.text.TextFilter)
Michael B.
thanks @Michael B i have edited my post.
Vivart
ThankQ Vivart its working fine
upendra