Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

number checks 1

Status
Not open for further replies.

yama

Programmer
Jun 28, 2001
69
SG
Hi,
does anyone know how to allow a user to enter only numbers in a textbox and would return an error if the user enters other characters?
thanks!
 
You can use javascript to validate but I prefer server-side validation. Check on the recieving JSP for valid input and if it is invalid resubmit to the input JSP and pass an error message. Here is a really simple example:
RECIEVING JSP:
Code:
<%
/* Do this at the top of the page */
String inputPageURL = &quot;/input.jsp&quot;;
int numberInput = 0;
try {
  numberInput = Integer.parseInt(request.getParameter(&quot;numberInput&quot;));
}
catch (NumberFormatException nfe) {
  request.setAttribute(&quot;errorMessage&quot;,&quot;Invalid Input&quot;);
  /* Redirect Request to Input Page */
  try {
    RequestDispatcher rd = request.getRequestDispatcher(inputPageURL);
    rd.forward(request,response)
  }
  catch (Exception e) {
    /* Do your error handling */
  }
}
/* REST OF PAGE */
%>
INPUT JSP:
Code:
<%
String errorMessage = request.getAttribute(&quot;errorMessage&quot;);
if (errorMessage != null) {
  out.println(errorMessage);
}
/* REST OF THE PAGE */
%>
Wushutwist
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top