11.07.2015 Views

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

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

CHAPTER 9 • STRINGS AND REGULAR EXPRESSIONSwww.it-ebooks.infoUsing Validate_USThe Validate_US package is very easy to use: simply instantiate the Validate_US() class and call theappropriate validation method. In total there are seven methods, four of which are relevant to thisdiscussion:phoneNumber(): Validates a phone number, returning TRUE on success and FALSEotherwise. It accepts phone numbers in a variety of formats, including xxx xxxxxxx,(xxx) xxx-xxxx, and similar combinations without dashes, parentheses, orspaces. For example, (614)999-9999, 61 49999999, and (614)9999999 are all valid,whereas (6149999999, 614-999-9999, and 614999 are not.postalCode(): Validates a ZIP code, returning TRUE on success and FALSE otherwise.It accepts ZIP codes in a variety of formats, including xxxxx, xxxxxxxxx, xxxxx-xxxx,and similar combinations without the dash. For example, 43210 and 43210-0362 areboth valid, whereas 4321 and 4321009999 are not.region(): Validates a state abbreviation, returning TRUE on success and FALSEotherwise. It accepts two-letter state abbreviations as supported by the U.S. PostalService (www.usps.com/ncsc/lookups/usps_abbreviations.html). For example, OH,CA, and NY are all valid, whereas CC, DUI, and BASF are not.ssn(): Validates an SSN by not only checking the SSN syntax but also reviewingvalidation information made available via the Social Security Administration Website (www.ssa.gov), returning TRUE on success and FALSE otherwise. It acceptsSSNs in a variety of formats, including xxx-xx-xxxx, xxx xx xxx, xxx/xx/xxxx,xxx\txx\txxxx (\t = tab), xxx\nxx\nxxxx (\n = newline), or any nine-digitcombination thereof involving dashes, spaces, forward slashes, tabs, or newlinecharacters. For example, 479-35-6432 and 591467543 are valid, whereas 999999999,777665555, and 45678 are not.Once you have an understanding of the method definitions, implementation is trivial. For example,suppose you want to validate a phone number. Just include the Validate_US class and call phoneNumber()like so:Because phoneNumber() returns a Boolean, in this example the Valid! message will be returned.Contrast this with supplying 614-876530932 to the method, which will inform the user of an invalidphone number.SummaryMany of the functions introduced in this chapter will be among the most commonly used within your<strong>PHP</strong> applications, as they form the crux of the language’s string-manipulation capabilities.The next chapter examines another set of commonly used functions: those devoted to working withthe file and operating system.228

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

Saved successfully!

Ooh no, something went wrong!