Php function to validate two decimal places of a number

Advertisement

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

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

Related Posts

» Getting random number between range of two numbers in JavaScript
» Display random number in random way using JavaScript
» W3c markup validation and Big websites – Is it really needed?
» Canada’s postal code validation in PHP

14 Comments on “Php function to validate two decimal places of a number”

  • Ginani Ahmad wrote on 19 April, 2008, 11:52

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

  • Roshan wrote on 19 April, 2008, 12:23

    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 ?

  • Ahmad Ginani wrote on 24 April, 2008, 4:10

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

  • Ahmad Ginani wrote on 24 April, 2008, 4:36

    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 ?

  • Roshan wrote on 24 April, 2008, 5:20

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

  • Sunil wrote on 10 May, 2008, 14:30

    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.

  • Roshan wrote on 10 May, 2008, 14:58

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

  • Sunil wrote on 11 May, 2008, 8:14

    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.

  • Roshan wrote on 11 May, 2008, 8:41

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

  • gowtham wrote on 10 September, 2008, 13:26

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

  • Ujjwal B Soni wrote on 30 December, 2008, 11:55

    Hi,

    Nice Article!!!

    Cheers!!!

    Ujjwal B Soni

  • sheathe wrote on 27 October, 2009, 8:27

    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!

  • Prabeen Giri wrote on 19 February, 2010, 6:53

    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.

Trackbacks

  1. PHP Coding School » Blog Archive » php tips [2008-02-10 10:55:27]

Write a Comment

 


Copyright © 2012 Roshan Bhattarai's Blog. All rights reserved.
Powered by WordPress.org, Custom Theme and ComFi.com Calling Card Company.