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.
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.
Learn how to restrict a user from entering certain characters into a text input using jQuery by performing the following steps:
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...