Php function to validate two decimal places of a number
Posted on February 10, 2008
Filed Under coding technique, php, tutorial
If you are looking for the validation of a number which contains only two decimal places. Means you want to accept the values like 0.21 or 1.34 or 12.55 or 445.66 as a input and throw an error when somebody enters the number like 0.2 or 4.678 from a text box. Here is a simple function for you in PHP which validates the number weather it contains exactly two decimal places or not.
Function to validate two decimal places of a number in PHP
function validateTwoDecimals($number)
{
if(ereg('^[0-9]+\.[0-9]{2}$', $number))
return true;
else
return false;
}
Well let me explain the fairly simple regular expression inside the ereg() function of PHP.
^[0-9]+\.[0-9]{2}$
The hat(^) represents the start of the string and the [0-9]+ tells that there will be one or more digits at the starting of the the string. “‘\.” represents that there should be a period(.) after that and [0-9]{2} tells that after there should be exactly two digits after period and the dollar sign($) represents the end of the string.
Popularity: 14% [?]
Follow me on twitter at http://twitter.com/roshanbh.
Related Posts
» Display random number in random way using JavaScript
» Canada’s postal code validation in PHP
» USA’s phone number validation using Regular expression in PHP
» Date format validation in PHP
Comments
10 Responses to “Php function to validate two decimal places of a number”
Leave a Reply






[...] Php function to validate two decimal places of a number By Roshan If you are looking for the validation of a number which contains only two decimal places. Means you want to accept the values like 0.21 or 1.34 or 12.55 or 445.66 as a input and throw an error when somebody enters the number like 0.2 or … Roshan Bhattarai’s Blog - PHP… - http://roshanbh.com.np [...]
Thank you very much, you have saved lots of my time, I was needing the value which should have 2 decimal point, if I get value 10 then it must be converted to 10.00…..thank you very much, finally I got the solution by following function :
function getFloat($number)
{
if(is_float($number))
{
if(ereg(”^[0-9]+\.[0-9]{1}$”, $number))
$number = $number.”0″;
else
$number = round($number, 2);
}
else
$number = $number.”.00″;
return $number;
}
Nice to hear that Giani…but you choose the very long way to do so…I think
echo number_format($number,2);
could have easily solved your problem rather than that long function…
Am I right ?
Yeah…., you are right, Thank you again Roshan..
Would you please help me for following problem ?
I want to prepare a php script by means of which user can search the string through out the current web site, but the site content is static, do not depend upon database……..
Could you please tell me how I start ?
Well for this kind of search….google’s custom search or google’s site search is better you…
Hello everyone.
I used the above mentioned script and it worked. But I have some difficulties. I want the script such that if suppose i input value as “sdga” then it should give error. And if i insert something like 562 it should not give error.
hey sunil for that you can use ctype_digit() function available in php
Thanxs very much Roshan.
I wld like to ask u, can I ans you other questions related to PHP. I mean not related to this numeric ones. I wld be thankful if u can help me.
Byee and have a nice day.
if something related to my blog post then no problem at all..