Welcome Guest, Not a member yet? Register   Sign In
Set a rule to validate decimal numbers
#1

[eluser]oualid[/eluser]
Hi.

I am wondering how to set a rule to validate a decimal number.
For example longitude 64.72796 or latitude 20.85733.

/Oualid
#2

[eluser]Phil Sturgeon[/eluser]
This should do the trick.

Code:
class MY_Form_Validation extends CI_Form_validation
{
    function decimal($str)
    {
        return (bool)preg_match('/^[\-+]?[0-9]+\.[0-9]+$/', $str);
    }
}
#3

[eluser]oualid[/eluser]
OK!

Tanks!
#4

[eluser]theprodigy[/eluser]
Quote:return (bool)preg_match('/^[\-+]?[0-9]+\.[0-9]+$/', $str);

I'm not a regex expert, so I figured I would ask.

Is there a difference between that listed above and

Code:
return (bool)preg_match('/^[\-+]?\d+\.\d+$/', $str);

Doesn't \d mean digit?
#5

[eluser]Sean Gates[/eluser]
Yes, they are equivalent.




Theme © iAndrew 2016 - Forum software by © MyBB