Jump to content

Allowing a comma in the price field


dk4210

Recommended Posts

Hello Guys,

 

I check the price field on the server side and I am trying to figure out how to allow a comma in the price field

 

example 2,300

 

Here is the code I use to check the price.

function check_Price($price,$member_id,$description,$ip){
if (isset($price) && (!is_numeric($price)))
{
      do something here
}

}

Thanks in Advance!

Dan

 

Link to comment
https://forums.phpfreaks.com/topic/237060-allowing-a-comma-in-the-price-field/
Share on other sites

I'd use str_replace along with ctype_digit to check it. If the data will be stored in a database, you don't want the comma in there anyhow.

 

if( ctype_digit(str_replace(',', '', $price)) ) {
	// value contains only decimal digits and commas, strip the commas and reassign the value (if needed)
                $price = str_replace( ',', '', $price );
} else {
	// value contains characters other than decimal digits and commas, so trigger an error message.
}

Hi Matthew Not sure how to incorporate that with my code.. Please advise

function check_Price($price,$member_id,$description,$ip){
if (isset($price) && (!is_numeric($price)))
{
      setlocale(LC_MONETARY, 'en_US');
      echo money_format('%i', $price) . "\n";
}

}

 

should work

 

No quite Matthew.The function is designed not to allow anything but number.

 

function check_Price($price,$member_id,$description,$ip){

if (isset($price) && (!is_numeric($price)))

{

      Echo "You submitted something other than a number"

}

 

}

I was mistaken regarding is_numeric() with form data; it will properly evaluate a numeric string, but are you aware that is_numeric will return TRUE for the following as well?

 

0xAF56D - Hexadecimal number

+10334.912E47 - Exponential number

 

If you don't consider those values to be valid, and I would suspect that you don't, you should not use is_numeric().

<?PHP
  function check_Price($price,$member_id,$description,$ip){
    if(intval($price) <= 0) { // Check if number is a number and above or equal to 1
      return 'You entered something other than a number.';
    } else {
      $price = number_format($price); // Format the number to add commas

      // Do other processing here.

    }
  }
?>

 

Try the above, you may need to tweak it to suit your needs, but it's a good start.

 

Regards, Paul Ryan.

Ok what would you use instead of is_numberic?

 

What if I wanted to just strip the comma?

 

When I added this code

 

function check_Price($price,$member_id,$description,$ip){
 $price = str_replace(',', '', $price);
if (isset($price) && (!is_numeric($price)))
{
      Echo "No";
}

}

 

If I enter in something like 24,555

 

I get just 24 in the database..It removes the comma but everything after words.

 

 

All you have to do is reassign the str_replace()d value to the variable. Another question, since you're dealing with money, are you only dealing with whole currency units or ar you dealing with a decimal as well? IOW, is 2.54 a valid value?

Apologies, I totally mis-read the post.

 

Remove the following line, and use the variable $price as the number.

$price = number_format($price);

 

It will remove anything that isn't a number, and convert a string with no numbers to 0

 

Regards, PaulRyan.

<?PHP
  function check_Price($price,$member_id,$description,$ip){
    if(strpos($price,'.') || preg_match('/[^0-9,]/',$price)) {
      return 'You did not enter a valid number.';
    } else {
      $price = preg_replace('/[^0-9]/','',$price);  // Remove commas for furthur processing

      // Other precessing here?   
    }
  }
?>

 

Try the above, tell me how it goes.

 

Regards, PaulRyan.

So then only whole numbers, between 10000 and 800000 are valid, correct?

 

$price = $_POST['in'];
function CHECK_PRICE($price) {
$temp = str_replace(',', '', $price);
if( !ctype_digit($temp) || (intval($temp) < 10000 || intval($temp) > 800000) ) {
	// VALUE IS INVALID
	return 'Number must be a whole number between 10,000 and 800,000';
} else {
	$price = str_replace(',', '', $price);
	return $price;
}
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.