Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter validation--how to limit numerical value?

I simply need to add a validation class that limits a numerical entry from being greater than 24.

Is this possible with CI's default validation classes or will I have to write a custom validation class?

like image 861
Kevin Brown Avatar asked Dec 01 '22 11:12

Kevin Brown


2 Answers

You can use validation rule "greater_than[24]"

like for Example

$this->form_validation->set_rules('your_number_field', 'Your Number', 'numeric|required|greater_than[24]');
like image 153
Arif Avatar answered Dec 04 '22 08:12

Arif


There's no maximum or minimum comparison function in the Form Validation Rule Reference, so you can just write your own validation function.

It's pretty straightforward. 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'
    );
like image 26
zombat Avatar answered Dec 04 '22 10:12

zombat