Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

validating html form data in JSP

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
de byrne
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry man, i got no idea what your talking about :S
 
Ranch Hand
Posts: 541
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try validator framework with Struts 1.1 very powerful validation framework for web applications
thanks
M Bhat
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic