Book Image

Mastering jQuery

By : Alex Libby
Book Image

Mastering jQuery

By: Alex Libby

Overview of this book

<p>Mastering jQuery has been written not only to help maximize your skills with core functionality in the library, but also to explore some of the more intriguing ways of using the library to achieve real-world solutions that could feature on any website or online environment.</p> <p>You'll start with a look at some of the more advanced ways to incorporate the library into your pages, followed by working with forms and advanced form validation using regular expressions. Next you'll move on to animating in jQuery, advanced event handling, and using jQuery effects.</p> <p>Finally, you will develop practical examples of using jQuery with external functionality such as node-webkit, before finishing with a session on optimizing your version of the library for maximum efficiency and exploring best practices for using QUnit.</p>
Table of Contents (21 chapters)
Mastering jQuery
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Building a simple validation plugin


Until now, our examples have been based around individual fields, such as an e-mail address or a website URL. The code is heavily duplicated, which makes for a bloated and inefficient solution.

Instead, let's completely flip our approach and turn our code into a generic plugin. We'll use the same core process to validate our code, depending on the regex that has been set within the plugin.

For this next exercise, we'll use a plugin produced by Cedric Ruiz. Although it is a few years old, it illustrates how we can create a single core validation process that uses a number of filters to verify the content entered in our form. Let's make a start by performing the following steps:

  1. From the code download that accompanies this book, extract copies of the quickvalidate.html, info.png, and quickvalidate.css files and save them in your project folder.

  2. Next, we need to create a plugin. In a new file, add the following code, saving it as jquery.quickvalidate.js, within...