A Basic Approach to Server-side Data Validation with AJAX
(Page 1 of 5 )
Would you like to learn how to validate user-supplied data using JavaScript, but without the risks? This article will show you how to handle it in a way that takes advantage of JavaScript's pluses, but adds a level of safety, thanks to AJAX. It is the first of three parts.
A downloadable file for this article is available
here.
Validating user-supplied data has been for years a highly discussed topic within the huge area of Web development. What really makes this subject an interesting thing is its significant grade of evolution during the last few years, being specifically empowered by the capacities of the different server-side scripting languages.
While validating online forms with JavaScript is a feature that can still be found on numerous websites, the truth is that this approach delivers extremely unsafe and unreliable results, particularly if the data being submitted affects some sensitive areas of those sites. Of course what I just said isn't breaking news to you, and quite probably you've been using your own server-side validation routines for a long time; as a result, JavaScript doesn't fit into your scheme any longer.
Nevertheless, one of the most appreciated advantages of using JavaScript-based validation rests mainly on its quick response, which makes it very suitable for notifying the user as to what data is wrong, as soon as he/she has finished typing a value on a specific form field. Unfortunately, as with everything in life, this responsiveness comes at a cost, and the drawback of this approach consists of its complete dependency on using scripting-enabled browsers, in order to perform the validation on the data in question.
But it's not all bad news when it comes to using JavaScript for checking user input. With the help of AJAX and a handy group of server-side validation routines, it's possible to have a relatively fast system, which can inform the user via JavaScript when invalid data was entered on an online form, while the real validation is performed on the server, with the scripting language of your choice.
Aside from the advantages that I mentioned before, this validation mechanism can be developed bearing in mind specific conditions where JavaScript has been disabled by the client, while still maintaining its full validation capabilities on the server. Indeed, this approach can be quite useful for constructing richer Web applications, and that's precisely what I'm going to do in the next few lines: build an AJAX-driven form validation application, which will use PHP for validating data on the server.
Of course, due to the versatility of the application that I plan to build, you can use your favorite server-side scripting language instead of PHP, and keep the same functionality. Okay, I'll stop wasting your precious time and go straight to showing you how to create this little AJAX-based application. Let's get going!
Next: A picture is worth a thousand words: defining the look and feel of the application >>
More JavaScript Articles
More By Alejandro Gervasio