Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Preg_match and monetary values 2

Status
Not open for further replies.

Sleidia

Technical User
May 4, 2001
1,284
FR

Hi guys,

Anyone knows the syntaxe to use in order to allow only integers and decimals using either "." or ","? No length limitation whatsoever.

Ie :

12 : OK
1,2 : OK
1.2 : OK
0,02 : OK
0.02 : OK
0.2345 : OK
12273273927392 : OK


Thanks :)
 
Thanks bam720 :)

Unfortunately, I'm not good (and patient) enough to find out how to modify it for my own purpose :(

That's why I've written a simple/dirty/longer function :

Code:
function hd_number_isprice($string) {

$tot_dot = substr_count($string, ".");
$tot_comma = substr_count($string, ",");
  
    // - ! - if decimal
    if (($tot_dot == 1 OR $tot_comma == 1) AND $tot_dot + $tot_comma < 2) {
    
        if (preg_match("^[0-9]+[0-9]+[0-9]$", str_replace(".", "", str_replace(",", "", $string)))) {
        
        return true;
        
        } else {
        
        return false;
        
        }
    
    // - ! - if not decimal
    } else {
    
        if (preg_match("^[0-9]+[0-9]+[0-9]$", $string)) {
        
        return true;
        
        } else {
        
        return false;
        
        }
    
    }

}
 
oooops ... here is the working one :

Code:
function hd_number_isprice($string) {

$tot_dot = substr_count($string, ".");
$tot_comma = substr_count($string, ",");
  
    // - ! - if decimal
    if (($tot_dot == 1 OR $tot_comma == 1) AND $tot_dot + $tot_comma < 2) {
    
        if (ereg("^[0-9]*$", str_replace(".", "", str_replace(",", "", $string)))) {
        
        return true;
        
        } else {
        
        return false;
        
        }
    
    // - ! - if not decimal
    } else {
    
        if (ereg("^[0-9]*$", $string)) {
        
        return true;
        
        } else {
        
        return false;
        
        }
    
    }

}
 
I think it might break on a case like this however....
123,456,789.01
123
Now I don't know if you will be expecting that or not, but your if statement at the top would find that to be "not decimal" as it has 2 commas + 1 dot = 3... My best guess for that would be
Code:
if (($tot_dot == 1 OR $tot_comma == 1) OR ($tot_dot + $tot_comma < 2))

I think that should take care of it... just a simple logical operator change...
 
Hi bam720 ;)

Thanks for your advice.

Well, I prefer to avoid formats like 123,456,789.01 because :
1) I find them way too confusing.
2) it's a value that will be stored in a decimal type database column (10,2).

As for the integer case, I've fully tested the function and it didn't produce any unpredictable result.

Thanks again :)
 
I was just bringing it up as a suggestion because if it is for user input then you never know what some crazy user will do :) If its for personal use then you're all good
 
would this be a neater regex?
Code:
if (isset($_POST['number'])){
 $output = trim($_POST['number']);
 $pattern = "/^[-+]?([0-9]*[,\.])?[0-9]+$/";
 if ( preg_match($pattern, $output)) { echo "match"; } else {echo "no match";};
}
?>
<form action="<?=$_SERVER['PHP_SELF']?>" method=post>
<input name="number" type="text" />
<input type="submit" name="submit" value="test"/>
</form>

this will match floating point numbers using either the dot or the comma as a decimal point equivalent. it will also match integers and handle positive and negative numbers.
 

Thanks Jpadie :)
It's true that it makes the code much shorter ahaha ;)

One quick question : if I want to remove the positive/negative feature, would the correct pattern be :
"/^([0-9]*[,\.])?[0-9]+$/"
?
 
yup. translated this means:

^ must be the start of the string
( ) is a group
[0-9] means must be a numeral
* means zero or more times
[,\.] means must have a comma or a dot (note the dot is escaped)
? means the previous item must be included zero or one times
[0-9] means the dot/comma (if any) must be followed by a numeral and
+ means one or more times
$ asserts that this must be the end of the string

 
Thanks,

Those infos can be found on PHP.net but putting all the pieces together in the right order is where the difficulty lies in my case :(
 
there are some applications that help you build regex. regexbuddy is one.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top