27.10.2015 Views

AJAX and PHP

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

4<br />

<strong>AJAX</strong> Form Validation<br />

Validating input data is an essential requirement for quality <strong>and</strong> secure software applications. In<br />

the case of web applications, validation is an even more sensitive area, because your application is<br />

widely reachable by many users with varying skill sets <strong>and</strong> intentions.<br />

Validation is not something to play with, because invalid data has the potential to harm the<br />

application's functionality, <strong>and</strong> even corrupt the application's most sensitive area: the database.<br />

Input data validation means checking whether the data entered by the user complies with<br />

previously defined rules, which are established according to the business rules of your application.<br />

For example, if you require dates to be entered in the YYYY-MM-DD format, then a date of<br />

"February 28" would be considered invalid. Email addresses <strong>and</strong> phone numbers are other<br />

examples of data that should be checked against valid formats.<br />

Carefully define the input data validation rules in the software requirements document of<br />

the application you're developing, <strong>and</strong> then use them consistently to validate your data!<br />

Historically, web form validation was implemented mostly at the server side, after the form was<br />

submitted. In some cases, there was also some JavaScript code on the client that performed simple<br />

validation such as checking whether the email address was valid, or if a user name had been entered.<br />

The problems encountered with traditional web form validation techniques are:<br />

• Server-side form validation meets the limits of the HTTP protocol, which is a<br />

stateless protocol. Unless special code is written to deal with this issue, after<br />

submitting a page containing invalid data, the user is shown back an empty form that<br />

has to be filled from scratch.<br />

• When submitting the page, the user needs to wait for a full page reload. For every<br />

mistake that is made when filling the form, a new page reload happens.<br />

In this chapter, we will create a form-validation application that implements the good old traditional<br />

techniques <strong>and</strong> adds an <strong>AJAX</strong> flavor, thereby making the form more user-friendly <strong>and</strong> responsive.<br />

Even if you implement <strong>AJAX</strong> validation, server-side validation is m<strong>and</strong>atory, because the server is<br />

the last line of defense against invalid data. The JavaScript code that gets to the client can not only be<br />

disabled permanently from the browser's settings, but it also can be easily modified or bypassed.<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!