Welcome Guest, Not a member yet? Register   Sign In
Form Validation (extentions) Greater than / Less than a curtain number
#1

[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.
#2

[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));
}
#3

[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.
#4

[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




Theme © iAndrew 2016 - Forum software by © MyBB