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