Book Image

jQuery 2.0 Development Cookbook

By : Leon Revill
Book Image

jQuery 2.0 Development Cookbook

By: Leon Revill

Overview of this book

Table of Contents (17 chapters)
jQuery 2.0 Development Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Restricting input character length


It is possible to limit the characters a user is able to input into a text field by utilizing jQuery's keypress events. In some situations, this can be a powerful user experience feature, as the user is visually aware of the characters that they cannot provide instead of having to wait for a response from the server informing them of this error.

Getting ready

Once again, we are going to need a blank HTML document with the latest version of jQuery to work through this recipe. Create recipe-6.html and ensure that you have jQuery downloaded and ready to go.

How to do it…

Learn how to restrict a user from entering certain characters into a text input using jQuery by performing the following steps:

  1. Add the following HTML code to your newly created recipe-6.html file that creates a basic HTML web page with a single input element:

    <!DOCTYPE html>
    <html>
    <head>
        <title>Chapter 2 :: jQuery Events</title>
        <script src="jquery.min...