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.

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..

Mr.Roshan thanks very much, very simple but powerfull once again thanks….

Hi,

Nice Article!!!

Cheers!!!

Ujjwal B Soni

Thanks for your article. How would I check for a dollar amount that may or may not have a decimal and may or may not have two decimal places. For example, all of the following are acceptable:

100

100.5

100.50

100.55

1000

No characters besides the decimal and numbers are allowed (no boolean either).

It can also not have more than 2 decimals.

e.g. 100.555 is not acceptable (if this is difficult, it could be trimmed)

Thanks in advance!

For Seathe:

There is simple modification required in the function written by Roshan Bro to acquire your result. Here it goes.

function validateTwoDecimals($number)

{

if(ereg(‘^[0-9]+\.[0-9]{3,}$’, $number))

return true;

else

return false;

}

In the regular expression above, {3,} checks if the character is 3digit character or more.