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

Enter your email address and get recent tutorials, tips, tricks and scripts of PHP, Ajax, JavaScript and CSS directly delivered to you email inbox:

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”

  1. PHP Coding School » Blog Archive » php tips [2008-02-10 10:55:27] on February 10th, 2008 11:09 am

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

  2. Ginani Ahmad on April 19th, 2008 11:52 am

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

  3. Roshan on April 19th, 2008 12:23 pm

    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 ?

  4. Ahmad Ginani on April 24th, 2008 4:10 am

    Yeah…., you are right, Thank you again Roshan..

  5. Ahmad Ginani on April 24th, 2008 4:36 am

    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 ?

  6. Roshan on April 24th, 2008 5:20 am

    Well for this kind of search….google’s custom search or google’s site search is better you…

  7. Sunil on May 10th, 2008 2:30 pm

    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.

  8. Roshan on May 10th, 2008 2:58 pm

    hey sunil for that you can use ctype_digit() function available in php

  9. Sunil on May 11th, 2008 8:14 am

    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.

  10. Roshan on May 11th, 2008 8:41 am

    if something related to my blog post then no problem at all..

Leave a Reply