Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CodeIgniter Validation Rules for Email and Password

I was used to coding application with CodeIgniter and I'm a total newbie. I just started to learn CodeIgniter 3.0, and and reached to validation rules.

I notice that xss_clean is now gone from the validation class, so what rules should I use in the validation of email and password? Using just trim, valid_email, and required is enough for security?

Sorry if that question has been asked, but I searched around and I see old topics where people is using xss_clean.

like image 276
Mohamed Ebrahim Avatar asked Jun 08 '15 11:06

Mohamed Ebrahim


People also ask

How to set validation rules in CodeIgniter?

Setting Validation Rules CodeIgniter lets you set as many validation rules as you need for a given field, cascading them in order, and it even lets you prep and pre-process the field data at the same time. To set validation rules you will use the set_rules() method: $this->form_validation->set_rules();

How to set custom error message in CodeIgniter form validation?

PHP_EOL; $data['err'] = $err; $this->load->view('viewname', $data); } else if ($this->form_validation->run() == true ) { #code... } else.. after setting your custom message to $err variable, print it on your view. Save this answer.


1 Answers

Include Email Helper:

$this->load->helper('email');

For Email:

$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|xss_clean');

Or you can also use PHP Filter for email validation as

filter_var($email, FILTER_VALIDATE_EMAIL);

For Password Expression

   public function chk_password_expression($str)

    {

    if (1 !== preg_match("/^.*(?=.{6,})(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z]).*$/", $str))

    {
        $this->form_validation->set_message('chk_password_expression', '%s must be at least 6 characters and must contain at least one lower case letter, one upper case letter and one digit');
        return FALSE;
    }

    else

    {
        return TRUE;
    }
} 

To call the function you should use:

$this->form_validation->set_rules(  'password', 'Password', 'trim|required|min_length[6]|max_length[15]|callback_chk_password_expression');

Note: chk_password_expression should be in same class controller or in parent class. Email helper should be included as $this->load->helper('email');

like image 195
Deep Kakkar Avatar answered Sep 19 '22 19:09

Deep Kakkar