Validating Mandatory Input


 function checkNotEmpty($s) {    return (trim($s) !== '');  } 


When validating form fields (see Chapter 4 for more about HTML forms), several checks can be done. However, you should test as little as possible. For instance, when recently trying to order concert tickets for a U.S. concert, I always failed because it expected a U.S. telephone number, which couldn't be provided.

The best check is to check whether there is any input at all. However, what is considered to be "any input"? If someone enters just whitespace (that is, space characters and other nontext characters), is the form field filled out correctly?

The best way is to use trim() before checking whether there is anything inside the variable or expression. The function trim() removes all kinds of whitespace characters, including the space character, horizontal and vertical tabs, carriage returns, and line feeds. If, after that, the string is not equal to an empty string, the (mandatory) field has been filled out.

NOTE

The file check.php contains sample calls and all following calls to validation functions in the file check.inc.php.





PHP Phrasebook
PHP Phrasebook
ISBN: 0672328178
EAN: 2147483647
Year: 2005
Pages: 193

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