validation - 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:
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.
解决方案
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:
推荐阅读
- mongodb - 隐藏密码和登录
- react-native - uri到react-native中的真实路径
- azure-active-directory - 错误请求:尝试使用 SAML 断言从 Azure 访问令牌时出现 400
- android - Android MiniPaint App 添加清除选项以清除画布
- google-chrome - Firefox 和 Chrome 的可访问性树的区别
- bash - 有没有办法从反汇编程序输出中只过滤掉汇编代码?
- python - 新的、干净的 venv 项目(Python 3 /IntelliJ IDEA)中的包要求 - 为什么有?
- reactjs - 如何在拖动标签中使用 onChange 属性?
- c# - c# webapi中如何处理和excel从前端(网站)发送的文件?
- asp.net-core - .net核心中间件自动设置内容类型为application/json;字符集=utf-8