CodeIgniter Forums
Form Validation (extentions) Greater than / Less than a curtain number - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Form Validation (extentions) Greater than / Less than a curtain number (/showthread.php?tid=43164)



Form Validation (extentions) Greater than / Less than a curtain number - El Forum - 07-02-2011

[eluser]xerosis[/eluser]
Hi,
I am quite new to working with codeigniter framework and was wondering if someone can help me add to the form validation.

Basically when someone fills up a form I need it to check and make sure the number they entered are:

- Greater than X
- Less than Y

How can i add a new part to the current existing form validation of codeigniter to do this?

Thanks.


Form Validation (extentions) Greater than / Less than a curtain number - El Forum - 07-02-2011

[eluser]osci[/eluser]
you can use from form validation the already defined rules greater_than and less than

or you can combine them in one rule extending form_validation
is_between[4.15] for 4 < val < 15

Code:
public function is_between($str,$val)
{
   if (! is_numeric($str))
   {
      return FALSE;
   }

   list($val, $max)=explode('.', $val);
   return (($str > $val) AND ($str < $max));
}



Form Validation (extentions) Greater than / Less than a curtain number - El Forum - 07-02-2011

[eluser]xerosis[/eluser]
Thanks a lot.

For some reason the greater_than and less_than did not work, I will go with this other option and make a callback.


Form Validation (extentions) Greater than / Less than a curtain number - El Forum - 07-15-2011

[eluser]Kiddo[/eluser]
dunno if anyone modified it yet or not, but i changed the greater and less_than validation with this:

function greater_than($str, $field)
{
if ( ! is_numeric($str))
{
return FALSE;
}
if ( ! is_numeric($field))
{
$min = $_POST[$field];
}
else $min = $field;
return $str > $min;
}

the logic is captured by function matches. i modified it cause i'm comparing some number with other number in 2 inputs field. Tongue. by doing this, i can validating my own number or from input field.

regards,
Kiddo