Home arrow PHP arrow Page 3 - Building a More Capable Validation Helper Class

Adding methods for validating IPs, email addresses and URLs - PHP

In this sixth part of an eight-part series on helper classes, I finish developing the validation helper class created in the previous part. We'll add even more useful validation methods to expand its functionality.

  1. Building a More Capable Validation Helper Class
  2. Review: the ValidatorHelper class
  3. Adding methods for validating IPs, email addresses and URLs
  4. The full source code for the improved validation helper class
By: Alejandro Gervasio
Rating: starstarstarstarstar / 4
August 24, 2009

print this article



Certainly, there’s plenty of room to extend the capacities of the validation helper. In its current state, the class is only capable of validating numbers and alphabetic and alphanumeric values, which isn’t enough even for a sample class like this.

So, to extend the functionality of the helper, I’m going to code three brand new methods, aimed at validating IP and email addresses and URLs as well, by taking advantage (again) of the capabilities given by the PHP filter extension.

That being explained, I suggest that you take a look at these methods, whose  signatures have been listed below. Here they are:

// validate URL

public function validate_url($url)





// validate IP address

public function validate_ip($ip)


return filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4);



// validate email address

public function validate_email($email)


return filter_var($email, FILTER_VALIDATE_EMAIL);


As you can see, the three methods shown above are extremely easy to grasp; they behave like basic wrappers for some popular PHP filters. In the first case, as its name suggests, the “validate_url()” method is responsible for checking whether or not a supplied URL is valid, while in the second example, the “validate_ip()” method performs this same task with IP addresses.

Finally, the last method is tasked with checking email addresses at a basic level, by using the FILTER_VALIDATE_EMAIL filter. That was really easy to understand, right?

Of course, if you’re pretty familiar with working with PHP filters, then you’ll realize that the previous methods can be easily modified to perform a more strict validation process on each supplied parameter. But for the moment I’ll keep them this simple, so the class’s code remains uncluttered and more readable.

Well, at this point I provided the validation helper class with the ability to validate IP and email addresses, in addition to checking URLs. So it's time to put all of the pieces together and list the helper’s full source code, including the three new methods that you just learned.

This process will be carried out in the last section of this tutorial. Please click on the link shown below and read the final segment.

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