Home arrow PHP arrow Page 3 - Building a PHP5 Form Processor: Coding the Form Validator Module

Testing numeric values and ranges: defining the “validateNumber()” and “validateRange()” methods - PHP

In this second part of a three-part series, we take up the topic of server-side validation. By the time you finish this article, you'll have the general guidelines for how to build a form validating class. You'll use some PHP built-in introspection functions, along with regular expressions, to assist you in building this class.

  1. Building a PHP5 Form Processor: Coding the Form Validator Module
  2. Checking for empty and integer values: looking at the “validateEmpty()” and “validateInteger()” methods
  3. Testing numeric values and ranges: defining the “validateNumber()” and “validateRange()” methods
  4. The big challenge: checking email addresses with the “validateEmail()” method
  5. Checking for errors: defining the “checkErrors()” and displayErrors()” methods
By: Alejandro Gervasio
Rating: starstarstarstarstar / 13
January 23, 2006

print this article



Indeed, another useful method to determine whether an user has entered a numeric value or not, is the “validateNumber()” method. Closely similar to the methods defined earlier, its source code looks like this:

public function validateNumber($field,$errorMessage){

In this case the method will check if the value entered is a numeric string and accordingly store the appropriate error message in the $errors array. Despite its simplicity, I’ve found myself very often using this method on forms where only numeric values are allowed.

Now, take a look at another handy method, “validateRange()”, which can be very useful for checking if the value entered on a field is within a range of pre-defined boundaries. Its definition is listed below:

public function validateRange


Right, admittedly this method is very simple, so I won’t stop long on it. The only thing worthwhile of being denoted is the assignation of default values for the corresponding boundaries, which can be easily changed, in order to meet different validation requirements.

In a similar fashion, if the input supplied by an user is out of the given boundaries, an error message will be added as a new element of the $errors array. As you can see, adding more validation methods to the class is just a matter of writing the appropriate checking routines and storing error messages according to the type of test performed on the input data.

Having defined the methods responsible for checking numeric strings and ranges of values entered on forms, the next thing to be done is writing a couple more, so the class can be capable of validating alphabetic and alphanumeric strings.

Validating alphanumeric and alphabetic values: defining the “validateAlphabetic()” and “validateAlphanum()” methods

Checking for alphabetic and alphanumeric characters is really a straightforward task that can be easily performed by utilizing PHP regular expressions. Moreover, if you’ve been working with regular expressions for a while, you’ll realize that writing more methods to validate input that must meet a particular format is a breeze. Verifying postal codes or phone numbers is reduced to coding the corresponding string pattern. Based on this ability, the two methods listed below verify alphabetic and alphanumeric characters in input strings:

public function validateAlphabetic($field,$errorMessage){

public function validateAlphanum($field,$errorMessage){

Definitely you’ll agree that these methods are simple to code and read. This ability to use regular expressions to validate user input comes in particularly handy, since it’s possible to encode complex string patterns and perform strict validation on user-supplied data. As you can see, the above listed methods demonstrate this concept, by encoding simple string patterns in order to check for alphabetic and alphanumeric characters in input data. In both cases, if the input string fails the test, the respective error messages are stored in the $errors array, as shown in previous class methods.

Certainly, I could spend more time coding additional methods to check whether input data fits a particular format. Since this process is reduced to tweaking string patterns in regular expressions, however, I won’t waste your precious time writing boring expressions. Instead, I’ll show you the last validation class method, which is quite useful for checking email addresses. So, jump into the next section to find out more about how this is done.

>>> More PHP Articles          >>> More By Alejandro Gervasio

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: