首页 > 解决方案 > input type number restriction to an INTEGER in jsp

问题描述

I have a fill.jsp file, in which I have the below code:

<div class="user-values">           <!-- for user's monthly gross pay -->
    <label for="grossPay">Monthly gross pay: </label>
    $<input id="grossPay" type="number" name="grossPay" placeholder="0.0 or greater" step="0.01" value="${grossPay}">
    <span class="req">*</span>
</div>

<div class="user-values">           <!-- for error message -->
    <p class="msg">${message2}</p>
</div>
            
<div class="user-values">           <!-- for user's dependents -->
    <label for="noOfDependents">Number of dependents: </label>
    <input id="noOfDependents" type="number" name="dependents" placeholder="0 or greater" step="1" value="${dependents}">
    <span class="req">*</span>
</div>

<div class="user-values">           <!-- for error message -->
    <p class="msg">${message3}</p>
</div>

The "Monthly gross pay" accepts double values. The "Number of Dependents" should accept only integers.

When I run this code: if I fill the number 2 or 2. for "Number of Dependents" and hit calculate, the calculation is processed successfully by my servlet.

However, if I fill the number of double value 2.1 or 2.2 or 3.1, and so on, I get the below error in my form:

JSP Form error

MY FIRST PROBLEM: When I enter the number of double value 2.0 or 3.0 or 4.0, etc., I get a NumberFormatException

Type: class java.lang.NumberFormatException
Message: For input string: "2.0"

Why am I not getting the error similar to the image above? Or, why is my processing not successful with 2.0? Please suggest a solution so that I can avoid the exception for values such as 1.0 or 2.0 or 3.0, and so on?

MY SECOND PROBLEM: The value part in both input types in my fill.jsp file is giving me the below warning in NetBeans IDE:

"Bad value " " for attribute "value" on element "input": Expected a minus sign or a digit but saw " " instead."

How can I try to avoid this warning?

Please note: my program runs properly even with the warning.

标签: validationjspservletsjakarta-eenumberformatexception

解决方案


I figured out the answer:

In my servlet I had a part of code: The "dependents" input box on the client-side takes in a value of 2.0, which is received as a String at the server-side.

String dependentNum = request.getParameter("dependents");
user.setDependents(Integer.parseInt(dependentNum));

For some reason, the second line of code was throwing a NumberFormatException for me. So, I changed the second line in my servlet code to:

user.setDependents((int) Double.parseDouble(dependentNum));

I don't know why the Integer object was having a problem with accepting the String value of 2.0 and converting it to 2. The workaround was to just convert the String value 2.0 to a double and cast it to an int, and I avoid getting the NumberFormatException completely.

However, I am still unable to understand, why can't the same validation error message show up as in the image for double values like 2.0, 3.0, when it can show for double values like 2.1, 3.2, etc., as below:

Validation error


推荐阅读