Codeigniter + requires letters and numbers in a password

I would like to use form validation to request a password that has BOTH alpha and numeric characters. Here is what I have come up with so far:

$this->form_validation->set_rules('password', 'Password', 'required|matches[passconf]|min_length[8]|alpha_numeric'); 

The problem is that "alpha_numeric" requires the password to contain only letters or numbers, they do not require both. Let's go here with a stronger password.

+4
source share
3 answers

You can configure callback in your controller:

 public function password_check($str) { if (preg_match('#[0-9]#', $str) && preg_match('#[a-zA-Z]#', $str)) { return TRUE; } return FALSE; } 

Then update your rule to use it:

 $this->form_validation->set_rules('password', 'Password', 'required|matches[passconf]|min_length[8]|alpha_numeric|callback_password_check'); 
+13
source

An alternative solution that can be easily used in other projects and localizes your validation rules is to extend the Form_validation class.

In the / library / application, create a file called MY_Form_validation.php, where "MY" is your subclass prefix defined in the /config/config.php application.

The class will look something like this:

 class MY_Form_validation extends CI_Form_validation { /** * Verify that a string contains a specified number of * uppercase, lowercase, and numbers. * * @access public * * @param String $str * @param String $format * * @return int */ public function password_check($str, $format) { $ret = TRUE; list($uppercase, $lowercase, $number) = explode(',', $format); $str_uc = $this->count_uppercase($str); $str_lc = $this->count_lowercase($str); $str_num = $this->count_numbers($str); if ($str_uc < $uppercase) // lacking uppercase characters { $ret = FALSE; $this->set_message('password_check', 'Password must contain at least ' . $uppercase . ' uppercase characters.'); } elseif ($str_lc < $lowercase) // lacking lowercase characters { $ret = FALSE; $this->set_message('password_check', 'Password must contain at least ' . $lowercase . ' lowercase characters.'); } elseif ($str_num < $number) // lacking numbers { $ret = FALSE; $this->set_message('password_check', 'Password must contain at least ' . $number . ' numbers characters.'); } return $ret; } /** * count the number of times an expression appears in a string * * @access private * * @param String $str * @param String $exp * * @return int */ private function count_occurrences($str, $exp) { $match = array(); preg_match_all($exp, $str, $match); return count($match[0]); } /** * count the number of lowercase characters in a string * * @access private * * @param String $str * * @return int */ private function count_lowercase($str) { return $this->count_occurrences($str, '/[az]/'); } /** * count the number of uppercase characters in a string * * @access private * * @param String $str * * @return int */ private function count_uppercase($str) { return $this->count_occurrences($str, '/[AZ]/'); } /** * count the number of numbers characters in a string * * @access private * * @param String $str * * @return int */ private function count_numbers($str) { return $this->count_occurrences($str, '/[0-9]/'); } } 

Then set the rule:

 $this->form_validation->set_rules('password', 'Password', 'required|matches[passconf]|min_length[8]|alpha_numeric|password_check[1,1,1]'); 

A rule can be used in many ways. 'password_check [1,1,1]' will require a password containing lowercase, uppercase and numeric characters. 'password_check [5,0,1]' will require 5 uppercase characters and a password number.

The advantages of this method are as follows:

  • All your rules are contained in an easily portable class.
  • Auxiliary verification functions, such as "count_occurences", can be easily used in other verification functions.
  • The class can be changed to use language files in the application / language /
+11
source

Using CI 2.2.0, there are parameters that you can add, for example, the "alpha_numeric" parameter, pay attention to the channel parameters (cascading):

 function check_password(){ $this -> form_validation -> set_rules('password', 'New Password', 'required|max_length[15]|min_length[6]|alpha_numeric'); $this -> form_validation -> set_rules('passconf', 'Re-enter', 'required|matches[password]'); if ($this -> form_validation -> run() == FALSE){ doStuff(); } else { doOtherStuff(); } } 

Look for the link to the rule at the bottom of this page.

-1
source

Source: https://habr.com/ru/post/1411664/


All Articles