Exception Handling in JavaScript: Validating forms Introduction
(Page 1 of 4 )
Here you have it. Welcome to the part four of the series “Exception handling in JavaScript.” Comprised of several parts, this tutorial introduces the usage of exceptions in JavaScript, by explaining the core concepts and demonstrating practical examples for fast application in numerous client-based programs.
Over the previous parts of the series I presented some examples aimed at illustrating in a very friendly way, how errors triggered by user-provided input can be caught by generic error handlers and manipulated accordingly.
In all the practical cases you saw, I used either a web form or a regular JavaScript prompt method for entering data, which was evaluated by the script. This is a helpful technique for trapping most of the primary error types. Of course, the only point worth noting here is the usage of the “try-catch” blocks for delegating error handling to specific pieces of code, while the rest of the program’s functions are focused on performing regular tasks within the execution of the program.
So far, I’ve explored the nice capabilities of JavaScript exceptions by studying the topic from a didactical point of view, rather than implementing a “real world” example, which might be considered for use in production environments. Hopefully, now that you’ve got a fairly solid grounding about working with exceptions, the next step will be applying some of the concepts that you learned in an old favorite application: form validation.
Throughout the rest of the article, I’ll explain through an illustrative example how exceptions can be used to perform client-side form verification. Even when this approach can be considered as an alternative method to regular form validating techniques, it’s worth it have a look at this implementation.
With the preliminaries out of the way, let’s get going.
Next: Setting the basics of form validation: defining custom error objects >>
More JavaScript Articles
More By Alejandro Gervasio