Interacting with PHP for Server-side Data Validation with AJAX - Extending the functionality of the checking class: defining some additional methods
(Page 3 of 4 )
In accordance with my own expectations (and yours, of course) of the functionality the previous PHP class should have, below I listed a few additional methods. These are handy for checking alphabetic and alphanumeric values, along with email addresses. Check them out:
// validate alphabetic field
public function validateAlphabetic($field,$value,$errorMessage)
{
if(!isset($value)||!preg_match("/^[a-zA-Z]+$/",$value)){
return $field.'|'.$errorMessage;
}
else{
return $field.'| ';
}
}
// validate alphanumeric field
public function validateAlphanum($field,$value,$errorMessage){
if(!isset($value)||!preg_match("/^[a-zA-Z0-9]+$/",$value)){
return $field.'|'.$errorMessage;
}
else{
return $field.'| ';
}
}
// validate email
public function validateEmail($field,$value,$errorMessage){
if(!isset($value)||!preg_match("/.+@.+..+./",$value)||!
checkdnsrr(array_pop(explode("@",$value)),"MX")){
return $field.'|'.$errorMessage;
}
else{
return $field.'| ';
}
}
// validate email (Windows systems)
public function validateEmailWin($field,$value,$errorMessage){
if(!isset($value)||!preg_match("/.+@.+..+./",$value)||!
$this->windnsrr(array_pop(explode("@",$value)),"MX")){
return $field.'|'.$errorMessage;
}
else{
return $field.'| ';
}
}
// private method 'windnsrr()' for Windows systems
private function windnsrr($hostName,$recType=''){
if(!empty($hostName)){
if($recType=='')$recType="MX";
exec("nslookup -type=$recType $hostName",$result);
foreach($result as $line){
if(preg_match("/^$hostName/",$line)){
return true;
}
}
return false;
}
return false;
}
// call validation method on the fly
public function callValMethod(){
eval("echo $this->validate".$this->method."('".$this-
>field."','".$this->value."','".$this->message."');");
}
As shown above, I added some extra methods to expand the existing capacity of the PHP checking class. Aside from performing traditional verification of user-supplied data, notice how the class calls the corresponding validation method on the fly, as illustrated below:
// call validation method on the fly
public function callValMethod(){
eval("echo $this->validate".$this->method."('".$this-
>field."','".$this->value."','".$this->message."');");
}
By using the properties that I explained in the previous section, the class is now capable of determining what checking method to use, and certainly what value to pick up for performing the validation process. As you'll probably agree, the "eval()" PHP built-in function is very useful in these cases.
At this point, I believe that you've grasped the fundamentals of how the data entered into the online form is validated with each HTTP request made in the background, and how the class sends the response text back to the client. Therefore, let's move one step forward and see how the client and server-side layers of the AJAX-based application fit together.
To learn how this will be done, please jump into the next section and keep reading.
Next: Assembling the pieces: completing the AJAX-driven checking application >>
More Java Articles
More By Alejandro Gervasio