Home arrow PHP arrow Page 4 - A PHP Validation Helper Class in Action

Validating alphabetic and alphanumeric values, IP and e-mail addresses, and URLs - PHP

If youíre an enthusiastic PHP developer interested in learning how to build helper classes, you've come to the right place. This article series will teach you in a few simple steps how to build several kinds of helper classes for manipulating strings, generating dynamic URLs and validate incoming data, and so forth.

TABLE OF CONTENTS:
  1. A PHP Validation Helper Class in Action
  2. Review: the previous ValidatorHelper class
  3. Checking integers and float numbers with the ValidatorHelper
  4. Validating alphabetic and alphanumeric values, IP and e-mail addresses, and URLs
By: Alejandro Gervasio
Rating: starstarstarstarstar / 3
August 31, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

To finish showing you how to use the validation helper class, the last thing that Iím going to do in this tutorial will be coding some additional examples that demonstrate how to use the classís remaining methods for checking alphabetic and alphanumeric values, IP and email addresses, and URLs as well.

That being clarified, hereís the set of code samples that perform the aforementioned validation tasks:

$validator = new ValidatorHelper();

 

// example on validating an alphabetic value

if ($validator->validate_alpha('a1234') === FALSE)

{

echo 'Input value is not a valid alphabetic value.';

}

else

{

echo 'Input value is a valid alphabetic value.';

}

/*

displays the following

Input value is not a valid alphabetic value.

*/

 

// example on validating an alphanumeric value

if ($validator->validate_alphanum('a1234') === FALSE)

{

echo 'Input value is not a valid alphanumeric value.';

}

else

{

echo 'Input value is a valid alphanumeric value.';

}

/*

displays the following

Input value is a valid alphanumeric value.

*/

 

// example on validating a URL

if ($validator->validate_url('http://devshed.com') === FALSE)

{

echo 'Input value is not a valid URL.';

}

else

{

echo 'Input value is a valid URL.';

}

/*

displays the following

Input value is a valid URL.

*/

 

// example on validating an IP address

if ($validator->validate_ip('127.0.0.1') === FALSE)

{

echo 'Input value is not a valid IP address.';

}

else

{

echo 'Input value is a valid IP address.';

}

/*

displays the following

Input value is a valid IP address

*/

 

// example on validate an email address

if ($validator->validate_email('info@domain.com') === FALSE)

{

echo 'Input value is not a valid email address.';

}

else

{

echo 'Input value is a valid email address.';

}

/*

displays the following

Input value is a valid email address

*/

As you can see above, validating email and IP addresses, alphabetic values and the like by using the interface provided by the helper class is a simple process that doesnít offer major difficulties. The whole procedure is reduced to calling dynamically the appropriate method and nothing else. Itís that easy.

Finally, before you move on and read the conclusion, Iíd like to point out again that all of the methods that make up this helper class can be largely improved, to perform a more strict validation on a given parameter.

I cited before the case of the ďvalidate_email()Ē method, which certainly can be completely refactored by using other PHP functions, but this same concept may be applied to other methods of the class as well. So, if youíre bored and want to test your PHP skills, then try to create your own enhanced version of the previous validation helper.

Final thoughts

Thatís all for now. In this seventh episode of the series, I demonstrated with a decent number of practical examples how to use this sample validation helper to verify integer and float numbers, IP and email addresses and so forth. As you saw previously, this process was very easy to follow, even if you have an average background in object-oriented programming with PHP 5.

And speaking of OOP, a good coding habit of this paradigm is to avoid unnecessary instantiation of classes. You can apply this principle to this specific case, since thereís no need to deal with a validation helper object when itís possible to call its methods statically.

Demonstrating how to accomplish this will be the subject of the last installment of the series, so now 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: