-
wolfgang1983 Senior Member
   
-
Posts: 627
Threads: 271
Joined: Oct 2014
Reputation:
7
11-05-2015, 06:38 PM
(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!
-
pdthinh Member
  
-
Posts: 106
Threads: 0
Joined: Jan 2015
Reputation:
8
(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; }
-
wolfgang1983 Senior Member
   
-
Posts: 627
Threads: 271
Joined: Oct 2014
Reputation:
7
(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!
-
Martin7483 Crossfire CMS
   
-
Posts: 373
Threads: 14
Joined: Sep 2015
Reputation:
20
(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]');
-
wolfgang1983 Senior Member
   
-
Posts: 627
Threads: 271
Joined: Oct 2014
Reputation:
7
11-06-2015, 01:46 PM
(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!
-
mwhitney Posting Freak
    
-
Posts: 1,101
Threads: 4
Joined: Nov 2014
Reputation:
95
(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
-
wolfgang1983 Senior Member
   
-
Posts: 627
Threads: 271
Joined: Oct 2014
Reputation:
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!
|