Welcome Guest, Not a member yet? Register   Sign In
[Solved] Regex Check Codeigniter Form Validation
#1

(This post was last modified: 11-06-2015, 02:50 PM by wolfgang1983.)

In codeigniter callback form validation

How can I check if $password has at least one upper case letter?

PHP Code:
<?php

class Login extends CI_Controller {

    public function 
__construct() {
        
parent::__construct();
    }

    public function 
index() {    
        
$data['title'] = 'Administration';

        
$this->form_validation->set_rules('username''Username''trim|required');
        
$this->form_validation->set_rules('password''Password''trim|required|callback_regex');

        if (
$this->form_validation->run() == FALSE) {

            
$this->load->view('common/header.tpl'$data);
            
$this->load->view('common/login.tpl'$data);
            
$this->load->view('common/footer.tpl');

        } else {

            
redirect('common/dashboard');

        }
    }

    public function 
regex($password) {
        if (
$password == FALSE) {
            
$this->form_validation->set_message('regex''Password must contain at least one upper case letter');
            return 
false;
        } else {
            return 
true;
        }
    }

There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply
#2

(11-05-2015, 06:38 PM)wolfgang1983 Wrote: In codeigniter callback form validation

How can I check if $password has at least one upper case letter?

PHP Code:
<?php

class Login extends CI_Controller {

 public function 
__construct() {
 
parent::__construct();
 }

 public function 
index() { 
 
$data['title'] = 'Administration';

 
$this->form_validation->set_rules('username''Username''trim|required');
 
$this->form_validation->set_rules('password''Password''trim|required|callback_regex');

 if (
$this->form_validation->run() == FALSE) {

 
$this->load->view('common/header.tpl'$data);
 
$this->load->view('common/login.tpl'$data);
 
$this->load->view('common/footer.tpl');

 } else {

 
redirect('common/dashboard');

 }
 }

 public function 
regex($password) {
 if (
$password == FALSE) {
 
$this->form_validation->set_message('regex''Password must contain at least one upper case letter');
 return 
false;
 } else {
 return 
true;
 }
 }


You could check for ascii code of each char in password, for ex:

PHP Code:
function hasCapital($str) {
 
       $found false;
 
       for ($i 0$i strlen($str); $i++) {
 
           if (ord($str[$i]) >= 65 && ord($str[$i]) <= 90) {
 
               $found true;
 
               break;
 
           }
 
       }
 
       return $found;

Reply
#3

(11-05-2015, 08:32 PM)pdthinh Wrote:
(11-05-2015, 06:38 PM)wolfgang1983 Wrote: In codeigniter callback form validation

How can I check if $password has at least one upper case letter?

PHP Code:
<?php

class Login extends CI_Controller {

 public function 
__construct() {
 
parent::__construct();
 }

 public function 
index() { 
 
$data['title'] = 'Administration';

 
$this->form_validation->set_rules('username''Username''trim|required');
 
$this->form_validation->set_rules('password''Password''trim|required|callback_regex');

 if (
$this->form_validation->run() == FALSE) {

 
$this->load->view('common/header.tpl'$data);
 
$this->load->view('common/login.tpl'$data);
 
$this->load->view('common/footer.tpl');

 } else {

 
redirect('common/dashboard');

 }
 }

 public function 
regex($password) {
 if (
$password == FALSE) {
 
$this->form_validation->set_message('regex''Password must contain at least one upper case letter');
 return 
false;
 } else {
 return 
true;
 }
 }


You could check for ascii code of each char in password, for ex:

PHP Code:
function hasCapital($str) {
 
       $found false;
 
       for ($i 0$i strlen($str); $i++) {
 
           if (ord($str[$i]) >= 65 && ord($str[$i]) <= 90) {
 
               $found true;
 
               break;
 
           }
 
       }
 
       return $found;


That worked but is there away to have it in a MY_Form_validation library so do not have to enter it as call back?
There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply
#4

(11-06-2015, 02:29 AM)wolfgang1983 Wrote: That worked but is there away to have it in a MY_Form_validation library so do not have to enter it as call back?

Just add the method to your MY_Form_validation library.
To use it set a rule that calls this method

PHP Code:
$this->form_validation->set_rules('password''Password''required|hasCapital'); 

if you would like to pass an extra value to this or any other custom validation rule use []
PHP Code:
$this->form_validation->set_rules('password''Password''required|hasCapital[some_extra_value]'); 
Reply
#5

(This post was last modified: 11-06-2015, 02:14 PM by wolfgang1983.)

I have placed the function in my form validation library but I get a error message:

I would like when false message to say. You need to have at least one upper case letter

PHP Code:
<?php

class MY_Form_validation extends CI_Form_validation {
    
    function 
hasCapital($str) {
 
       $found false;
 
       for ($i 0$i strlen($str); $i++) {
 
           if (ord($str[$i]) >= 65 && ord($str[$i]) <= 90) {
 
               $found true;
 
               break;
 
           }
 
       }
 
       return $found;
    } 


Error Message:


Code:
Unable to access an error message corresponding to your field name Password.(hasCapital)

Controller

PHP Code:
<?php

class Login extends CI_Controller {

    public function 
__construct() {
        
parent::__construct();
    }

    public function 
index() {    
        
$data['title'] = 'Administration';

        
$this->form_validation->set_rules('username''Username''trim|required');
        
$this->form_validation->set_rules('password''Password''trim|required|hasCapital');

        if (
$this->form_validation->run() == FALSE) {

            
$this->load->view('common/header.tpl'$data);
            
$this->load->view('common/login.tpl'$data);
            
$this->load->view('common/footer.tpl');

        } else {

            
redirect('common/dashboard');

        }
    }

Update:
I have tried this now. Just would like to know if correct?

PHP Code:
<?php

class MY_Form_validation extends CI_Form_validation {

   function 
has_capital($str) {
    if (
$this->regex($str) == FALSE) {
           
$this->set_message('has_capital''Your ' .$str' needs to have at least one upper case letter');
       return 
false;
    } else {
       return 
true;
    }
    } 
    
    function 
regex($str) {
 
       $found FALSE;

 
       for ($i 0$i strlen($str); $i++) {
 
           if (ord($str[$i]) >= 65 && ord($str[$i]) <= 90) {
 
               $found true;
 
               break;
 
           }
 
       }

 
       return $found;
    } 

There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply
#6

(11-06-2015, 01:46 PM)wolfgang1983 Wrote: Update:
I have tried this now. Just would like to know if correct?

PHP Code:
<?php

class MY_Form_validation extends CI_Form_validation {

 
  function has_capital($str) {
    if (
$this->regex($str) == FALSE) {
 
          $this->set_message('has_capital''Your ' .$str' needs to have at least one upper case letter');
     
  return false;
    } else {
     
  return true;
    }
 
   
    
 
   function regex($str) {
 
       $found FALSE;

 
       for ($i 0$i strlen($str); $i++) {
 
           if (ord($str[$i]) >= 65 && ord($str[$i]) <= 90) {
 
               $found true;
 
               break;
 
           }
 
       }

 
       return $found;
 
   


The set_message() call should probably be:
PHP Code:
$this->set_message('has_capital''Your {field} needs to have at least one upper case letter'); 


If you want to display what they input, you can use {param} in your message text. See http://www.codeigniter.com/user_guide/li...r-messages
Reply
#7

(11-06-2015, 02:34 PM)mwhitney Wrote:
(11-06-2015, 01:46 PM)wolfgang1983 Wrote: Update:
I have tried this now. Just would like to know if correct?

PHP Code:
<?php

class MY_Form_validation extends CI_Form_validation {

 
  function has_capital($str) {
    if (
$this->regex($str) == FALSE) {
 
          $this->set_message('has_capital''Your ' .$str' needs to have at least one upper case letter');
     
  return false;
    } else {
     
  return true;
    }
 
   
    
 
   function regex($str) {
 
       $found FALSE;

 
       for ($i 0$i strlen($str); $i++) {
 
           if (ord($str[$i]) >= 65 && ord($str[$i]) <= 90) {
 
               $found true;
 
               break;
 
           }
 
       }

 
       return $found;
 
   


The set_message() call should probably be:
PHP Code:
$this->set_message('has_capital''Your {field} needs to have at least one upper case letter'); 


If you want to display what they input, you can use {param} in your message text. See http://www.codeigniter.com/user_guide/li...r-messages

Thanks for that tip
There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply




Theme © iAndrew 2016 - Forum software by © MyBB