user3014926
user3014926

Reputation: 1099

how to check if user input is a double?

In my JSP page, I let users input their dollar balance. Then I catch this in servlet as: String input = req.getParameter("balance");

How do I check if input variable is double? I know how to use ParseDouble, but I don't want to catch the exception. Instead, I want to pass my own error message to JSP so that users can see it when they have typing error.

Can someone help me?

Upvotes: 2

Views: 1684

Answers (1)

Rahul Tripathi
Rahul Tripathi

Reputation: 172458

You may also create a function like this:

boolean isDouble(String input) {
        try {
            Double.parseDouble(input);
            return true;
        } catch (NumberFormatException e) {
            //You can send the message which you want to show to your users here.
            return false;
        }
    }

Upvotes: 4

Related Questions