Welcome Guest, Not a member yet? Register   Sign In
Is there a helper to validate URI variable?
#1

[eluser]rvillalon[/eluser]
Is there a helper to validate URI variable?

I'm trying to make sure the variable is an integer > 0. I can do it manually, but I was wondering if there was a helper that does this like the form validation helper?
#2

[eluser]n0xie[/eluser]
I use this helper function to check if the id is a natural number

Code:
if ( ! function_exists('is_natural'))
{
    function is_natural($str)
    {
           return (bool)preg_match( '/^[0-9]+$/', $str);
    }
}
#3

[eluser]rvillalon[/eluser]
Thanks, n0xie!

I also found example code in the cart library that's similar to what I wanted. Hopefully this will come in useful for someone in the future.

Code:
// Prep the quantity. It can only be a number.  Duh...
$items['qty'] = trim(preg_replace('/([^0-9])/i', '', $items['qty']));
// Trim any leading zeros
$items['qty'] = trim(preg_replace('/(^[0]+)/i', '', $items['qty']));

// If the quantity is zero or blank there's nothing for us to do
if ( ! is_numeric($items['qty']) OR $items['qty'] == 0)
{
    return FALSE;
}
#4

[eluser]garymardell[/eluser]
You could always just write it as a route, with the regex to check, or extend the router to add it as :validint. Or something. Similar to :num or :any.




Theme © iAndrew 2016 - Forum software by © MyBB