CodeIgniter Forums
Set a rule to validate decimal numbers - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Set a rule to validate decimal numbers (/showthread.php?tid=26564)



Set a rule to validate decimal numbers - El Forum - 01-17-2010

[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


Set a rule to validate decimal numbers - El Forum - 01-17-2010

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



Set a rule to validate decimal numbers - El Forum - 01-17-2010

[eluser]oualid[/eluser]
OK!

Tanks!


Set a rule to validate decimal numbers - El Forum - 01-17-2010

[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?


Set a rule to validate decimal numbers - El Forum - 01-18-2010

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