• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

form field validation

 
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have a small problem. I have a form that is taking in a credit card number. The servlet that the form is posted to is to check the credit card number for two things:

1. to make sure it's at least 15 digits for american express and maximum 16 for visa and mastercard.

2. make sure the field was not left blank.

I have the first task done. But I'm unsure how to ensure the field isn't left blank.

My servlet reads in the value from the form as follows:
long cardnumber = Long.parseLong(request.getParameter("ccnumber"));

How can I check if the field is left blank, which gives the result "".

P.s. I know I could use javascript on the jsp page to ensure a value is entered before the form can be submitted, but I'd rather not if possible.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look at the string version of the value before you try parsing it to a Long.

String value = request.getParameter("ccnumber");

Play with entering nothing, entering something and deleting it with backspace, delete with the delete key, any other combination you can think of. I think sometimes you'll find null and sometimes an empty string. At least our web folks always test for both.

Alternatively you might just catch the exception thrown by parseLong for invalid values like embedded spaces or letters or whatever. Try that with those input combinations, too.
[ April 19, 2005: Message edited by: Stan James ]
 
Jason Kwok
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah yes, that's brilliant, I don't know why I didn't think of that before. Thanks so much!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic