• Post Reply Bookmark Topic Watch Topic
  • New Topic

Validate fields with JavaScript or Servlet?  RSS feed

 
Barry Andrews
Ranch Hand
Posts: 529
C++ Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
My question is in 2 parts:
First, I was wondering if there is any benefit to validating a field using JavaScript or just letting the servlet do it? I want to first make sure the field is not blank before invoking the servlet. I was thinking that performance wise, this approach would be better, but I don't know. What do other's think.
Also, if I did allow JavaScript to validate the fields, how would I go about doing this? The form always tries to invoke the servlet before calling the JavaScript function. Is this possible to do?
Thanks for your help!
Barry
 
Barry Andrews
Ranch Hand
Posts: 529
C++ Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please disregard the second part of the question. I figured that one out. I would still like thoughts on the first though.
thanks,
Barry
 
Juanjo Bazan
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
About your first question: You should validate every field in your servlet. To do it using javascript is good for avoiding a lot of incorrect requests but javascript is really easy to 'bypass'. Everyone could call the servlet just writing the url in the address bar.
Best solution: Validate twice. Client and Server -side.
HTH
 
Barry Andrews
Ranch Hand
Posts: 529
C++ Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I never thought about that. Good point!

thanks,
Barry
 
Val Pecaoco
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Barry,
I would like to recommend that you use Struts (because I know someday you will, so why not do it now ), wherein you could automatically enable form validation using ActionForm objects. This will let you separate validation from your main business code (which is cool ). Usually what I do is double-layer validation - on the client side using JavaScript and on the server side using ActionForm objects. Either way it's a win-win situation.
Ex Animo Java!
-- Val
[ July 28, 2002: Message edited by: Val Pecaoco ]
 
georgejk
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hai
Yes I agree with My friend Mr.Val Pecaoco has pointed out. Struts gives you a frameowrk to do all of ur validations. Its very simple to learn also. You can set your own error messages for one particular validation.
visit http://jakarta.appache.org/struts/
for more information.
regards Georgie
 
Jessica Sant
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with the above posts -- I'd put it in JavaScript so that validation occurs on the Client-side (avoids processing on the server-side). However, if someone has JavaScript disabled -- you're hooped, so definately re-validate it on the Server-Side with Struts (will give you a new technology to learn/master too!)
----------------------------------------
"georgejk"-
Welcome to the JavaRanch! Please adjust your displayed name to match the JavaRanch Naming Policy.
You can change it here.
Thanks! and again welcome to the JavaRanch!
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!