Adding Server-side Capabilities to Form Validation with the DOM - Completing the form validation class
(Page 4 of 5 )
In consonance with the concepts discussed in the section that you just read, the next step that I’ll take now consists basically of adding some extra methods to the corresponding form validation class, which, as you know, was shown a few lines before.
Having said that, below you can see the complete signature of this class. I've included some extra methods for validating specific data, like alphanumeric values and email addresses, and for counting errors as well. Take a look at the definition of the respective class, please:
class FormValidator{
private $errors=array();
private $method;
const MIN=4;
const MAX=32;
public function __construct(){
$this->method=$_POST;
}
// validate empty values
public function validateEmpty($field,$errorMessage){
if(!$this->method[$field]||trim($this->
method[$field])==''||strlen($this->method[$field])
<self::MIN||strlen($this->method[$field])>self::MAX){
$this->errors[]=$errorMessage;
}
}
// validate integer values
public function validateInt($field,$errorMessage){
if(!$this->method[$field]||!is_numeric($this->
method[$field])||intval($this->method[$field])!=$this->method[$field]){
$this->errors[]=$errorMessage;
}
}
// validate numeric values
public function validateNumber($field,$errorMessage){
if(!$this->method[$field]||!is_numeric($this->method[$field])){
$this->errors[]=$errorMessage;
}
}
// validate range of values
public function validateRange($field,$errorMessage){
if(!$this->method[$field]||$this->
method[$field]<self::MIN||$this->method[$field]>self::MAX){
$this->errors[]=$errorMessage;
}
}
// validate alphanumeric values
public function validateAlphanum($field,$errorMessage){
if(!$this->method[$field]||!preg_match("/^[a-zA-Z0-9]+$/",
$this->method[$field])){
$this->errors[]=$errorMessage;
}
}
// validate email address
public function validateEmail($field,$errorMessage){
if(!$this->method[$field]||!preg_match
("/.+@.+..+./",$this->method[$field])||!checkdnsrr
(array_pop(explode("@",$this->method[$field])),"MX")){
$this->errors[]=$errorMessage;
}
}
// validate email address (Windows systems)
public function validateEmailWin($field,$errorMessage){
if(!$this->method[$field]||!preg_match(/^.+@.+..+$/,
$this->method[$field])||!$this->windnsrr(array_pop(explode("@",
$this->method[$field])),"MX")){
$this->errors[]=$errorMessage;
}
}
private function windnsrr($hostName,$recType=''){
if($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;
}
public function checkErrors(){
if(count($this->errors)>0){
return true;
}
return false;
}
public function displayErrors(){
$output='<ul>';
foreach($this->errors as $error){
$output.='<li>'.$error.'</li>';
}
$output.='</ul>';
return $output;
}
}
Okay, that's the complete signature of the form validation class.
In this case, the only detail worth noticing is the addition of some brand new methods, useful for checking specific data, as well as the definition of “checkErrors()” and “countErrors(),” handy for counting and displaying errors respectively.
At this point, and assuming that the form checking class that you saw before is now a clear concept in your mind, the final step that I plan to take simply consists of putting the DOM-based validation script created previously to work together with the PHP class in question. In this way, you’ll be able to have at your disposal a fully-featured form validation system, which not only checks for invalid data in the client, but it performs the corresponding validation process in the server too.
To see how these two modules of the application work in conjunction, please read the final section of this article. We’re almost finished!
Next: Assembling the client and server-side modules of the application >>
More JavaScript Articles
More By Alejandro Gervasio