4

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?

1
  • Just curious why this was downvoted? Commented Jan 4, 2010 at 3:21

3 Answers 3

11

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]');
Sign up to request clarification or add additional context in comments.

Comments

5

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'
    );

Comments

4

Sure you can, just make your own validation function and add it as a callback to validation rule. See http://codeigniter.com/user_guide/libraries/form_validation.html#callbacks

Hence, 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;
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.