Page 1 of 1

First try at OOP->Field checker

Posted: Fri Nov 21, 2008 4:13 am
by yoji
Hiz.. I have tried creating few sites in the past and I always get stuck with problems, and also that I find my self iterating when it comes to the text fields.. You know those sign-up pages. You gotta check user, pass, name length then pass should be valid, then pas.... yada yada yada.. So I created this class fieldchecker :

Code: Select all

 
<?php
class fieldschecker
{
    // 4-30 min and max email length
    // 4-30 min and max username length
    // 6-30 min and max password length
    // field_error stores every error occured
    // breaker is just for the sake of new line in showing errors
    public $field_error;
    public $yeserror=0;
    public $breaker="<br>";
    private $maxemaillength=30;
    private $minemaillength=4;
    private $maxpasswordlength=30;
    private $minpasswordlength=6;
    private $maxusernamelength=30;
    private $minusernamelength=3;
    private $maxnamelength=30;
    private $minnamelength=2;
    private $maxnamelength=30;
    private $minlastnamelength=2;
    private $mainpassword;// For checking the second confirm password
    
    //////////////////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////////////////  
 
    function namechecker($name)
    {
        if (strlen($name) <= $this->minnamelength)
        {
            $this->field_error.="Name length should be more than ".$this->minnamelength." characters.".$this->breaker;
            $this->yeserror=1;
        }
        
        if (strlen($name) >= $this->maxnamelength)
        {
            $this->field_error.="Name length should be less than ".$this->maxnamelength." characters.".$this->breaker;
            $this->yeserror=1;
        }
    }
    
    //////////////////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////////////////  
 
    function lastnamechecker($lastname)
    {
        if (strlen($lastname) <= $this->minlastnamelength)
        {
            $this->field_error.="Last name length should be more than ".$this->minlastnamelength." characters.".$this->breaker;
            $this->yeserror=1;
        }
        
        if (strlen($lastname) >= $this->maxlastnamelength)
        {
            $this->field_error.="Last name length should be less than ".$this->maxlastnamelength." characters.".$this->breaker;
            $this->yeserror=1;
        }
    }
    
    //////////////////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////////////////
    function emailchecker ($email)
    {
        if (strlen($email) <= $this->minemaillength)
        {
            $this->field_error.="Email length should be more than ".$this->minemaillength." characters.".$this->breaker;
            $this->yeserror=1;
        }   
        
        if (strlen($email) >= $this->maxemaillength)
        {
            $this->field_error.="Email length should be less than ".$this->maxemaillength." characters.".$this->breaker;
            $this->yeserror=1;
        }
        
        if (!filter_var($email, FILTER_VALIDATE_EMAIL))
        {
            $this->field_error.="Please provide a proper email.".$this->breaker;
            $this->yeserror=1;
        }
    }
    //////////////////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////////////////
    function passwordchecker($password)
    {
        $this->mainpassword=$password;
        if (strlen($password) <= $this->minpasswordlength)
        {
            $this->field_error.="Password length should be more than ".$this->minpasswordlength." characters.".$this->breaker;
            $this->yeserror=1;
        }
        
        if (strlen($password) >= $this->maxpasswordlength)
        {
            $this->field_error.="Email length should be less than ".$this->maxpasswordlength." characters.".$this->breaker;
            $this->yeserror=1;
        }
    }
    //////////////////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////////////////
    function passwordrechecker($repassword)
    {
        if ($this->mainpassword != $repassword)
        {
        $this->field_error.="Password and Confirm Password should be same.".$this->breaker;
            $this->yeserror=1;
        }
    }
    //////////////////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////////////////
    function usernamechecker($username)
    {
        if (strlen($username)<= $this->minusernamelength)
        {
            $this->field_error.="Username length should be more than ".$this->minusernamelength." characters.".$this->breaker;
        }
        
        if (strlen($username)>= $this->maxusernamelength)
        {
            $this->field_error.="Username length should be less than ".$this->maxusernamelength." characters.".$this->breaker;
        }
    }
    
    
}
?>
 
I started writing it yesterday.. It is my first shot at OOP. The code really made my work easy. Any suggestions guys? Any amendments I should make? I was thinking of creating another class of mysql, and this should be extended, that way I will be capable of comparing username from database to check whether it already exists... One more question, Is it useful to anybody else besides me :p

Re: First try at OOP->Field checker

Posted: Fri Nov 21, 2008 4:52 am
by papa
I've just had a quick look, but I did a simple error handler on my mysql class which stores the error messages in an array and then displays it.

I see that you have done something similar but with a string.

Maybe:

$this->field_error[]

And then create an error handler.

This yeserror feels redundant. If field_error contains data error will show otherwise no problemo.