Home arrow PHP arrow Page 4 - Creating a Validation Helper Class

Validating alphabetic and alphanumeric values - PHP

Helper classes seem easy to build -- almost deceptively so. If you're building a helper class to assist with a crucial function, however, such as validation, the approach you need to take may seem a bit less straightforward. This fifth article in an eight-part series on building helper classes walks you through the process, complete with hands-on examples.

TABLE OF CONTENTS:
  1. Creating a Validation Helper Class
  2. Review: a URL-handling helper class
  3. Creating a validation class
  4. Validating alphabetic and alphanumeric values
By: Alejandro Gervasio
Rating: starstarstarstarstar / 4
August 17, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

As I expressed in the section that you just read, I’m going to finish this installment of the series by adding two more methods to the “ValidatorHelper” class, which will be responsible for checking alphabetic and alphanumeric data.

So, in summary, the methods that will perform these tasks look like this:

// validate alphabetic value

public function validate_alpha($value)

{

return filter_var($value, FILTER_VALIDATE_REGEXP, array('options' => array('regexp' => "/^[a-zA-Z]+$/")));

}

 

// validate alphanumeric value

public function validate_alphanum($value)

{

return filter_var($value, FILTER_VALIDATE_REGEXP, array('options' => array('regexp' => "/^[a-zA-Z0-9]+$/")));

}

Again, I used the “filter_var()” function that comes with the PHP filter extension to give a concrete implementation to the two previous methods. In the first case, the method will validate alphabetic values, while in the second case, the other method will verify alphanumeric values. That was pretty simple to grasp, wasn’t it?

Well, now that you know how these two brand new methods work, it’s time to list the source code of the validation helper class, whose functionality has been extended a bit further. So, here’s how the class looks:

class ValidatorHelper

{

// constructor not implemented

public function __construct(){}

 

// validate integer

public function validate_int($value, $min, $max)

{

return filter_var($value, FILTER_VALIDATE_INT, array('options' => array('min_range' => $min, 'max_range' => $max)));

}

 

// validate float number

public function validate_float($value)

{

return filter_var($value, FILTER_VALIDATE_FLOAT);

 }

// validate alphabetic value

public function validate_alpha($value)

{

return filter_var($value, FILTER_VALIDATE_REGEXP, array('options' => array('regexp' => "/^[a-zA-Z]+$/")));

}

 

// validate alphanumeric value

public function validate_alphanum($value)

{

return filter_var($value, FILTER_VALIDATE_REGEXP, array('options' => array('regexp' => "/^[a-zA-Z0-9]+$/")));

}

}

There you have it. I don’t want to market the helper class to you, but admittedly it has become a more functional piece of code. Of course, there’s plenty of room to add more methods to the class if you need to validate other common data, such as IP and email addresses, strings and so forth. However, all of these features will be incorporated in the upcoming tutorial of the series.

For the moment, I encourage you to introduce your own enhancements to the previous helper class. In this way you will not only sharpen your existing programming skills, but get a more intimate knowledge of using the PHP filters extension.

Final Thoughts

In this fifth part of the series, I discussed the creation of a validation helper class in PHP 5, which was given the capability to check integers and float numbers, and alphabetic and alphanumeric values as well.

But, as you may have realized, it’s pretty easy to extend the capabilities of the class via PHP filters and add methods to it that check for email and IP addresses, strings, etc.

That will be exactly the topics that I plan to cover in the next tutorial, so you don’t have any excuses to miss it!



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- 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: