02.06.2013 Views

Pro PHP and jQuery by Jason Lengstorf.pdf - Computer Science ...

Pro PHP and jQuery by Jason Lengstorf.pdf - Computer Science ...

Pro PHP and jQuery by Jason Lengstorf.pdf - Computer Science ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

CHAPTER 9 ■ PERFORMING FORM VALIDATION WITH REGULAR EXPRESSIONS<br />

Armed with this regex pattern, you can now validate the date input in your application.<br />

Adding a Validation Method to the Calendar Class<br />

To validate the date string, you will add a new private method to the Calendar class called _validDate().<br />

This method will accept the date string to be validated, then compare it to the validation pattern<br />

using preg_match(), which returns the number of matches found in the given string. Because this<br />

particular pattern will only match if the entire string conforms to the pattern, a valid date will return 1,<br />

while an invalid date will return 0.<br />

If the date is valid, the method will return TRUE; otherwise, it will return FALSE.<br />

Add this method to the Calendar class <strong>by</strong> inserting the following bold code into<br />

class.calendar.inc.php:<br />

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

Saved successfully!

Ooh no, something went wrong!