Interacting with PHP for Server-side Data Validation with AJAX
Looking for new and creative ways to empower your AJAX-driven Web applications? Then you’ve landed at the right place. Welcome to the final part of the series “Server-side Data Validation with AJAX.” In three tutorials, this series demonstrates how to build a simple form checking system which uses AJAX to performing server-side validation on the data supplied by users.
Interacting with PHP for Server-side Data Validation with AJAX - Validating data on the server: building an input checking PHP class (Page 2 of 4 )
In order to sequentially validate the data entered into each form field every time an HTTP request is triggered, I'll create a highly generic input checking class, which can be used not only with this particular project, but in many others that you may have. If you're an experienced PHP developer, then the class that I'm going to define over the next few lines shouldn't be difficult to understand at all.
And speaking of that class in question, here's my first attempt at it. Please take a look at its signature:
class formValidator{ public function __construct(){ $this->method=$_GET['method']; $this->field=$_GET['field']; $this->value=$_GET['value']; $this->message=$_GET['message']; } // validate empty field public function validateEmpty ($field,$value,$errorMessage,$min=8,$max=32){ if(!isset($value)||trim($value)==''||strlen($value) <$min||strlen($value)>$max){ return $field.'|'.$errorMessage; } else{ return $field.'| '; } } // validate integer field public function validateInt($field,$value,$errorMessage){ if(!isset($value)||!is_numeric($value)||intval($value)! =$value){ return $field.'|'.$errorMessage; } else{ return $field.'| '; } } // validate numeric field public function validateNumber($field,$value,$errorMessage){ if(!isset($value)||!is_numeric($value)){ return $field.'|'.$errorMessage; } else{ return $field.'| '; } } // validate if field is within a range public function validateRange ($field,$value,$errorMessage,$min=1,$max=99){ if(!isset($value)||$value<$min||$value>$max){ return $field.'|'.$errorMessage; } else{ return $field.'| '; } } }
As you can appreciate, the PHP class that I defined above isn't rocket science at all. Aside from performing some useful initialization tasks inside the pertinent constructor, the class exposes some handy methods for checking the validity of different types of data.
Here, I've included the typical methods for checking empty strings, float and integer values, and specific ranges of values. All of them return a string divided by a (|) pipe character, depending on the result of the validation process.
However, before I proceed to add more useful methods to the PHP class, I'd like to highlight some things that are indeed important. Please notice the properties assigned within the constructor method. The question that comes up now is: what are they?
Right, in simple terms, each of the properties captures the corresponding GET variable passed with the query string when an HTTP request is made, in order to set up the following parameters:
$_GET['method'] (specifies the validation method for being used). $_GET['field'] (indicates the name of the form field for being validated). $_GET['value'] (specifies the value of the form field for being validated). $_GET['message'] (determines the error message for being displayed if an error occurs.)
As you can see, all the properties detailed above tell the class how to perform the corresponding validation process, which, as you learned in the previous article, will be executed with each HTTP request. Quite good, right?
Okay, after explaining the logic followed by the data checking class, it would be highly desirable to aggregate some other checking methods, so it will look a bit more complete and functional. For this reason, over the next section, I'll define these additional methods. They check for alphabetic and alphanumeric values, as well as for email addresses. Therefore, please continue reading.