Codeigniter check - how to limit a numerical value?

I just need to add a validation class that limits the numeric entry to 24.

Is this possible with the default CI authentication classes, or do I need to write my own authentication class?

+3
source share
3 answers

There is no maximum or minimum comparison function in the Link validation rule , so you can just write your own validation function .

It is pretty simple. Something like this should work:

function maximumCheck($num)
{
    if ($num > 24)
    {
        $this->form_validation->set_message(
                        'your_number_field',
                        'The %s field must be less than 24'
                    );
        return FALSE;
    }
    else
    {
        return TRUE;
    }
}


$this->form_validation->set_rules(
        'your_number_field', 'Your Number', 'callback_maximumCheck'
    );
+5
source

You can use the validation rule " greater_than[24]"

as an example

$this->form_validation->set_rules('your_number_field', 'Your Number', 'numeric|required|greater_than[24]');
+6

Of course, you can simply create your own validation function and add it as an answer to the validation rule. See http://codeigniter.com/user_guide/libraries/form_validation.html#callbacks

Therefore you will have

...
$this->form_validation->set_rules('mynumber', 'This field', 'callback_numcheck');
....
function numcheck($in) {
  if (intval($in) > 24) {
   $this->form_validation->set_message('numcheck', 'Larger than 24');
   return FALSE;
  } else {
   return TRUE;
  }
}
+3
source

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


All Articles