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) { return filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED); }
// 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.
blog comments powered by Disqus |
|
|
|
|
|
|
|