Mark Beleski
Mark Beleski

Reputation: 55

Validate HTML5 Numeric Input Before Sending up to Server

I'm writing a web based application that does calculations. Instead of the user filling out a form on a JSP and then getting the results on another JSP, I want to have an HTML page using HTML5 to ask the user for numeric input and then display the calculated output on a JSP. Is there any server side method to verify the user entered only numeric information? So far I used used 3 HTML5 input boxes like: <input type="number" name="width" min=".1" step=".1" max="500">

It appears the only validation it does is catch if the user enters numeric input under the min value or over the max value. If nothing is entered it still sends. I want it to not let you send the data unless all 3 boxes are valid. I know you can simply use JavaScript for this but it's a JSP class so my instructor wouldn't like if I use anything other than HTML or JSP. Thanks in advance.

EDIT

Here's the page they will get prompted with:

<form method="get" action="carpetprice.jsp"> Width of room in feet: <input type="number" name="width" min=".1" step=".1" max="500"><br /> Length of room in feet: <input type="number" name="length" min=".1" step=".1" max="500"><br /> Price of carpet per square foot: <input type="number" name="width" min=".1" step=".1" max="500"><br /> <input type="submit" value="Compute Carpet Price"> </form>

I dont want it to send any of the data to "carpetprice" until it has been fully validated.

Upvotes: 1

Views: 1524

Answers (1)

Celeritas
Celeritas

Reputation: 15033

On the page you have the form you need actually need a <form> tag. You should show all the code you working with. Here's an example:

<form method="POST" action="check.jsp">
<input type="number" name="width" min=".1" step=".1" max="500" name="someMeaningfulName" />
<input type="submit" />
</form>

Then in check.jsp you would have something like

<%
String toBeValidated = request.getParameter("someMeaningfulName");
//rest of the validation process here
%>

Use an if statement to check if the integer is in a certain range. You didn't say what you want to happen if the validation fails. Is the user redirected back to the page with the form on it?

Upvotes: 1

Related Questions