validation max_length, min_length in codeigniter validation max_length, min_length in codeigniter codeigniter codeigniter

validation max_length, min_length in codeigniter


Just add another %s to get the length value..

$this->form_validation->set_message('min_length', '%s: the minimum of characters is %s');

You will give the size only if you use %s second time. first time it will give field name you specified..


$this->form_validation->set_rules('password', 'Password', 'required|min_length[8]|max_length[20]';

is missing a closing bracket -- ):

$this->form_validation->set_rules('password', 'Password', 'required|min_length[8]|max_length[20]');


Update
Perhaps using a callback function would be better? E.g.:

$this->form_validation->set_rules('username', 'Username', 'required|xss_clean|callback__check_length[6,10]');/** * Callback function. Checks if the length of the user's input * conforms to minimum and maximum character length requirements * * @param  string * @param  int * @param  int * @return bool */function _check_length($input, $min, $max){    $length = strlen($input);    if ($length <= $max && $length >= $min)    {        return TRUE;    }    elseif ($length < $min)    {        $this->form_validation->set_message('_check_length', 'Minimum number of characters is ' . $min);        return FALSE;            }    elseif ($length > $max)    {        $this->form_validation->set_message('_check_length', 'Maximum number of characters is ' . $max);        return FALSE;            }}


When using set_message, you can't specify the lengths (both min and max) the way that you have done. You can either set a general message for min_length and max_length like:

$this->form_validation->set_message('min_length', 'The value you have entered for %s is too short');$this->form_validation->set_message('max_length', 'The value you have entered for %s is too long');

Otherwise, you will have to utilise CodeIgniter's callback feature and set up a custom validation function which would look something like this (for the minimum length!!).

public function custom_min_length($str, $val) {    if (preg_match("/[^0-9]/", $val)) {        return FALSE;    }    if (function_exists('mb_strlen')) {        if(mb_strlen($str) < $val) {            $this->form_validation->set_message('custom_min_length', '%s: The Minimum Number Of Characters Is ' . $val);            return FALSE;        } else {            return TRUE;        }    }    if(strlen($str) < $val) {        $this->form_validation->set_message('custom_min_length', '%s: The Minimum Number Of Characters Is ' . $val);        return FALSE;    } else {        return TRUE;    }}

You would set your validation rule like this:

$this->form_validation->set_rules('username', 'Username', 'callback_custom_min_length[6]|required|xss_clean');

Notice the callback_ prefix on the custom_min_length rule. This replaces, and is used instead of the usual min_length CI function.

Hope that helps...I'll leave you figure out how to do the custom_max_length function :)