Section A.15. Chapter 15


A.15. Chapter 15


Solution to Question 15-1

JavaScript's pros are users get immediate feedback when entering data into fields about that data's validity, and the form doesn't need to be redisplayed by the PHP code.

One of JavaScript's cons is that the data must still be validated in your PHP code because it's possible for a user to turn off JavaScript in her browser or for a malicious user to directly submit data to your form-processing script. Additionally, the validation doesn't have access to any of the server datafor example, session information or database information.


Solution to Question 15-2

To display the warning "The username field must be at least six characters," execute:

 alert("The username field must be at least six characters"); 


Solution to Question 15-3

Validate a U.S. zip code that may have the optional "plus four" style as follows:

 '/^\d{5}(-\d{4})?$/' 

Remember that the regex expression must be in Perl format, which starts with '/ and ends with /'.


Solution to Question 15-4

To test a variable called $zipcode using the regex from the last question, specify:

 <?php $pattern = '/^\d{5}(-\d{4})?$/'; $matched=preg_match($pattern, $zipcode, $matches); if ($matched) {   echo ("Zipcode OK."); } ?> 



Learning PHP and MySQL
Learning PHP and MySQL
ISBN: 0596101104
EAN: 2147483647
Year: N/A
Pages: 135

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net