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

Checking integers and float numbers with the ValidatorHelper - 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

In accordance with the concepts deployed in the section that you just read, it’s necessary to create some simple examples that show how to use the validation helper class in concrete cases. With that thought in mind, below I included two approachable code samples that use the helper for validating first an integer, and then a float number.

Here are the examples in question:

$validator = new ValidatorHelper();

 

// example on validating an integer value

if ($validator->validate_int(10.2, 1, 100) === FALSE)

{

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

}

else

{

echo 'Input value is a valid integer.';

}

/*

displays the following

Input value is not a valid integer.

*/

 

// example on validating a float value

if ($validator->validate_float(1234.5) === FALSE)

{

echo 'Input value is not a valid float number.';

}

else

{

echo 'Input value is a valid float number.';

}

/*

displays the following

Input value is a valid float number.

*/

Definitely, understanding the examples shown above is a pretty straightforward process. The “validate_int()” and “validate_float()” methods of the helper are called after creating an instance of it. Also, in this particular case I fed these methods with rather trivial values to make the examples easier to grasp, but logically more complex parameters can be used.

So far, so good, right? Now that you've grasped how to use the previous validation helper class for checking integer and float numbers, it’s time to continue evaluating its functionality. As you’ll recall, the class also has some other methods, which can be used for validating IP and email addresses, alphabetic and alphanumeric values and finally URLs.

So, in the last segment of this tutorial I’m going to demonstrate how to validate those specific types of data. So click on the link shown below and read the following lines. 



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
   

PHP ARTICLES

- 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...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 11 - Follow our Sitemap

Dev Shed Tutorial Topics: