File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes HTML, CSS and JavaScript and the fly likes AJAX  and simple form validation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "AJAX  and simple form validation" Watch "AJAX  and simple form validation" New topic
Author

AJAX and simple form validation

Andrew Mcmurray
Ranch Hand

Joined: Sep 24, 2005
Posts: 188
Hi all

I just started at a new place that uses AJAX. I am new to AJAX and have been doing my form validation in the past using struts validation. Does anybody know of any good tutorials on how to use AJAX for simple form validation. I looked online for a little while and there seems to be differnt ways, I wasn't sure which way was the best?

Thanks,

AMD
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Well, the first thing I would do is ask the people you are working with how they handle it since they are in fact, already using Ajax. If they don't have a good answer or you simply don't like their answer, then it depends.

Are you currently using any web frameworks (struts, jsf, etc)?
Are you using any Ajax frameworks (prototype, DWR, etc)?

Once those questions are answered it makes it easier to find a solution that fits with your development environment.

Now, with all that being said, I'll ask the big question...WHY USE AJAX FOR VALIDATION?

If you are using Ajax, then you are requiring Javascript. So why not handle the validation on the client? To that you say "we must perform server side validation as well because it's more secure that way". I'd say I agree. So why do you need ajax to do this?

Whether you are posting the data via ajax to the server or a typical post method without ajax, the server side code is the same. The difference is only in how you deliver the response.


GenRocket - Experts at Building Test Data
Andrew Mcmurray
Ranch Hand

Joined: Sep 24, 2005
Posts: 188
Hi Greg,

Thanks for your response. No we are not using struts, that is whatI was use to. The problem is right now there is no standard on how to validate forms here. I ran into a similar problem with another team and it tured into a giant headache later. I wanted to propose a standard way to validate forms early. The people here like AJAX so I figured if AJAX was a good solution then go for it.

Thanks,

AMD
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
What are we talking about with ASjax validation, does the form actually need it. COuld normal clientside validation cover it?

Questions that need to be asked since using Ajax can really put a burden on a server.

Eric
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61103
    
  66

Please say that you're not planning on forgoing server-side data validation upon submission just because you're not using Struts.

Eric's and Gregg's point is: you're going to have to validate all the data on the server when the form gets submitted anyways, so is it really necessary to go back to the server to do discrete validation on evey form element via Ajax?

Just becasue you are using Ajax in an application doesn't mean everything has to be done with Ajax. A lot of the old rules about when and how to do things in a web application still apply.
[ August 16, 2006: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Andrew Mcmurray
Ranch Hand

Joined: Sep 24, 2005
Posts: 188
Thanks guys. No we are going to do server side also. I was just wondering if AJAX on the front end would be a good choice.

AMD
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Originally posted by Andrew Mcmurray:
Thanks guys. No we are going to do server side also. I was just wondering if AJAX on the front end would be a good choice.

AMD


There is no such thing as "Ajax on the front end". It's a combination of client side scripting (JavaScript) and calls to the server (back end). IMHO, there is no real need to do form validation using Ajax techniques. You are tripling your effort.
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
Can I ask what type of Validation is it?

Eric
Andrew Mcmurray
Ranch Hand

Joined: Sep 24, 2005
Posts: 188
Just basic form stuff. Required fields, field lengths, email address and phone number formats.
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
Waste of bandwidth since you can do all of that on the client with normal JS validation. No need to get the server involved until the entire form is submitted.

If you want to do something such as ping the db to see if the user name is taken or that if the eemail was already used, than you can do that with Ajax so you can save the user a few moments. Doing everyday validation is a waste.

Eric
Andrew Mcmurray
Ranch Hand

Joined: Sep 24, 2005
Posts: 188
I see so basically just use js on the front, throw some validation exceptions on the back and good to go. Thanks

AMD
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: AJAX and simple form validation