File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes validating html form data in JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "validating html form data in JSP" Watch "validating html form data in JSP" New topic
Author

validating html form data in JSP

de byrne
Greenhorn

Joined: Oct 21, 2003
Posts: 3
hi,
i have created a form in html, with several input boxes where they enter numbers (currency). I then, get these results, and print them out in a jsp page.
However, atm, they can enter strings into the html text boxes, and i only want them to be able to enter numbers.
how can i go about performing a server-side validation for these input variables to ensure it only accepts numberical data?
[ October 21, 2003: Message edited by: de byrne ]
jyothsna kumari
Ranch Hand

Joined: Jul 21, 2003
Posts: 108
hai,
u can do this by performing server side validation.read the input text as a string and try to convert it to integer.if exception is thrown then it indicates that input value is not numbers.in this way u can perform validation on serverside and send the result to jsp page.
something similar to
try
{
if(text!=null && !text.equals(""))
{
int number=Integer.parseInt(text);
print("input text is integer");
}
}catch(Exception e){print("input text is not integer"); }
hope this helps u
jyothsna
Amit KumarS
Ranch Hand

Joined: Oct 10, 2003
Posts: 100
Hi,
there is client side way of doing this too... u can write a JavaScript function which will be called on click of Submit button in which u can call a predefined javascript method
isNaN() (stands for Not a Number)and pass the value of text box to it if it returns true then it is not a number else it is...
hope this will help you... for any further queries please revert back
Thanks
Amit


****************************<br />In 24 hrs Earth rotates once on its Axis.
de byrne
Greenhorn

Joined: Oct 21, 2003
Posts: 3
well, i tried inserting this in my jsp file :s no luck. i did change the attribues to their correct name (replacing "text" with clothingMon". Not sure how to go about doing this :S
Please advise.


that is what i have atm :S and when it discovers that the user did not input an int, it will display the option to go back
cheers
de
jyothsna kumari
Ranch Hand

Joined: Jul 21, 2003
Posts: 108
the code which i have given is for server side validation.i think u asked for server side validation.on clicking the submit button,u read the entered variables into session variables and then run the handler class.In the class u use the given code to validate the input text.if it is correct then go the remaining else redirect to the same page.
if u still have any problems,feel free to ask
jyothsna
de byrne
Greenhorn

Joined: Oct 21, 2003
Posts: 3
sorry man, i got no idea what your talking about :S
Tim Baker
Ranch Hand

Joined: Oct 04, 2003
Posts: 541
what you have there checks that the user has entered something, rather than checking it is a number. also you have some redundent code:
if ((clothingMon == null) || (clothingMon!= null && clothingMon.equals("")) ||
you can take out the clothingMon!= null && part because when the first part evaluates to true the rest of the expression will no be evaluated and if the first part is false then you dont have a problem. so just use
if ((clothingMon == null) || clothingMon.equals("") ||
to check they have entered numbers you need to try and convert it to an int like this
try
{
int iClothingMon =Integer.parseInt(clothingMon);
int iClothingTue = Integer.parseInt(clothingTue);
.....
}catch(Exception e)
{
// print out some other error like "you didnt enter all numbers"
}
you may want to use JavaScript in the HTML to check it before hand but always do it through JSP code aswell so that they can't bypass the checks. i wouldn't both with JavaScrip though


Kim Jong II (North Korea's Dear Leader) said:Nuclear weapons don't kill people, people kill people.
Manjunath Bhat
Greenhorn

Joined: Oct 22, 2003
Posts: 8
Try validator framework with Struts 1.1 very powerful validation framework for web applications
thanks
M Bhat
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: validating html form data in JSP