Skip to Content
Ajax Hacks
book

Ajax Hacks

by Bruce W. Perry
March 2006
Intermediate to advanced
440 pages
11h 21m
English
O'Reilly Media, Inc.
Content preview from Ajax Hacks

Chapter 3. Validation

Hacks 22–27

Validating the data that users provide in web forms is an important step in exchanging information with them. Web applications should not hit the network with blank form information or fields that contain nonnumeric characters where numbers (such as a credit card number or ZIP Code) are required. To prevent this, JavaScript provides an option to implement client-side validation: a form can self-check before the code connects with the server and submits the data. The client represents the first layer of validation an application can implement. A second layer of validation on the server is critically important, for example, when the data involves financial or other private information, or if JavaScript is disabled in the user’s browser. Server-side validation is required to ensure that the application handles valid data. The following hacks demonstrate validation techniques for blank text controls, email addresses, credit card numbers, and U.S. ZIP Codes.

Validate a Text Field or textarea for Blank Fields

Spare the network an unnecessary hit if the user leaves any required form fields blank.

No web developers want their Ajax applications to hit the network with requests if the users leave necessary text fields blank. Thus, checking that input elements of type text and the large boxes called textareas in HTML contain values is one of the most common forms of validation.

This hack shows the code for checking if a text control is blank. The inline

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Ajax on Rails

Ajax on Rails

Scott Raymond
Ajax Security

Ajax Security

Billy Hoffman, Bryan Sullivan

Publisher Resources

ISBN: 0596101694Purchase bookErrata Page