asmith Posted November 29, 2007 Share Posted November 29, 2007 how can i validate a number float with just 2 decimal . example : 5.01 valid 5 valid 5.001 invalid 0.05 valid 6650.5 valid 1.5 valid ! Link to comment https://forums.phpfreaks.com/topic/79383-solved-validate-float/ Share on other sites More sharing options...
~n[EO]n~ Posted November 29, 2007 Share Posted November 29, 2007 This falls under Regex http://www.phpfreaks.com/forums/index.php/board,43.0.html anyway , do this quick test <?php $test = array( "5.01", "5", "5.001", "0.05", "6650.5", "1.5", "2.323" ); // if there's a dot, min 1 no max 2 no.. $regex = '/^\d+(.\d{1,2})?$/'; // test numbers ... foreach ($test as $number) { if (preg_match($regex, $number)) { echo "valid "; } else { echo "invalid "; } echo $number."<Br>"; } ?> Link to comment https://forums.phpfreaks.com/topic/79383-solved-validate-float/#findComment-401871 Share on other sites More sharing options...
asmith Posted November 29, 2007 Author Share Posted November 29, 2007 thanks Link to comment https://forums.phpfreaks.com/topic/79383-solved-validate-float/#findComment-401873 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.