Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java textfield decimal validation

How can I validate user input contain only 5 digits with 2 decimal. I am using below code to check 5 digits, but how to check 2 decimal.

if (!id.equals("")) {
        try {
            Integer.parseInt(id);
            if (id.length() <= 5) {
                return true;
            }
        } catch (NumberFormatException nfe) {
            return false;
        }
    }
like image 309
user236501 Avatar asked Apr 19 '26 20:04

user236501


1 Answers

The DecimalFormat class will help you significantly with this. You can check that the user-input can be parsed according to a specific pattern.

The commons-validator library wraps this up in a convenient method call.

like image 138
matt b Avatar answered Apr 22 '26 09:04

matt b



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!