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!
blog comments powered by Disqus |
|
|
|
|
|
|
|