Welcome Guest, Not a member yet? Register   Sign In
[Solved] Regex Check Codeigniter Form Validation
#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


Messages In This Thread
RE: Regex Check Codeigniter Form Validation - by wolfgang1983 - 11-06-2015, 02:29 AM



Theme © iAndrew 2016 - Forum software by © MyBB