Journal

How to validate Alpha Numeric, special charecter and minimum 8digit password validation in CodeIgnetter ?

CodeIgniter Strong Password Validation:

public function forgotpassword(){    
        if(isset($_POST['submit']) && $_POST['submit']=='Submit'){    
            $valid = array(
                    array(
                    'field' => 'userpassword',
                    'label' => 'User password',
                    'rules' => 'callback_valid_password',
                    ),
                    array(
                        'field' => 'userpassword1',
                        'label' => 'Confirm Password',
                        'rules' => 'matches[userpassword]',
                    ),
                  );
            $this->form_validation->set_rules($valid);
            if($this->form_validation->run() == TRUE) {        
                //Your success thing goes here
            }else{
                 //Your error thing goes here
            }
        }
}

//Now create a function for checking password
public function valid_password($userpassword = ''){    
        $userpassword=trim($userpassword);    
        $regex_lowercase = '/[a-z]/';
        $regex_uppercase = '/[A-Z]/';
        $regex_number = '/[0-9]/';
        $regex_special = '/[!@#$%^&*()\-_=+{};:,<.>§~]/';        

        if(empty($userpassword)){
            $this->form_validation->set_message('valid_password', 'The password field is required.');
            return FALSE;
        }        
        if (preg_match_all($regex_lowercase, $userpassword) < 1){
            $this->form_validation->set_message('valid_password', 'The field must be at least one lowercase letter.');
            return FALSE;
        }
        if (preg_match_all($regex_uppercase, $userpassword) < 1){
            $this->form_validation->set_message('valid_password', 'The field must be at least one uppercase letter.');
            return FALSE;
        }
        if (preg_match_all($regex_number, $userpassword) < 1){
            $this->form_validation->set_message('valid_password', 'The field must have at least one number.');
            return FALSE;
        }
        if (preg_match_all($regex_special, $userpassword) < 1){
            $this->form_validation->set_message('valid_password', 'The field must have at least one special character.' . ' ' . htmlentities('!@#$%^&*()\-_=+{};:,<.>§~'));
            return FALSE;
        }
        if (strlen($userpassword) < 8){
            $this->form_validation->set_message('valid_password', 'The field must be at least 8 characters in length.');
            return FALSE;
        }
        if (strlen($userpassword) > 32){
            $this->form_validation->set_message('valid_password', 'The field cannot exceed 32 characters in length.');
            return FALSE;
        }
            return TRUE;    
}

Ad comes here
Comments
Leave A Comment
PHP blog