dk4210 Posted May 21, 2011 Share Posted May 21, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/237060-allowing-a-comma-in-the-price-field/ Share on other sites More sharing options...
Pikachu2000 Posted May 21, 2011 Share Posted May 21, 2011 Is this incoming form data? If you're using is_numeric() to check form data, it will always return FALSE. All form data is, by default, string type. Quote Link to comment https://forums.phpfreaks.com/topic/237060-allowing-a-comma-in-the-price-field/#findComment-1218478 Share on other sites More sharing options...
matthew9090 Posted May 21, 2011 Share Posted May 21, 2011 take a look at this http://www.w3schools.com/php/func_string_money_format.asp Quote Link to comment https://forums.phpfreaks.com/topic/237060-allowing-a-comma-in-the-price-field/#findComment-1218479 Share on other sites More sharing options...
dk4210 Posted May 21, 2011 Author Share Posted May 21, 2011 It is incoming data from a form field. The user types in the price. Currently it works unless the user adds a coma. What do you suggest? Quote Link to comment https://forums.phpfreaks.com/topic/237060-allowing-a-comma-in-the-price-field/#findComment-1218480 Share on other sites More sharing options...
dk4210 Posted May 21, 2011 Author Share Posted May 21, 2011 Hi Matthew Not sure how to incorporate that with my code.. Please advise Quote Link to comment https://forums.phpfreaks.com/topic/237060-allowing-a-comma-in-the-price-field/#findComment-1218484 Share on other sites More sharing options...
Pikachu2000 Posted May 21, 2011 Share Posted May 21, 2011 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. } Quote Link to comment https://forums.phpfreaks.com/topic/237060-allowing-a-comma-in-the-price-field/#findComment-1218485 Share on other sites More sharing options...
matthew9090 Posted May 21, 2011 Share Posted May 21, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/237060-allowing-a-comma-in-the-price-field/#findComment-1218487 Share on other sites More sharing options...
dk4210 Posted May 21, 2011 Author Share Posted May 21, 2011 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" } } Quote Link to comment https://forums.phpfreaks.com/topic/237060-allowing-a-comma-in-the-price-field/#findComment-1218489 Share on other sites More sharing options...
Pikachu2000 Posted May 21, 2011 Share Posted May 21, 2011 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(). Quote Link to comment https://forums.phpfreaks.com/topic/237060-allowing-a-comma-in-the-price-field/#findComment-1218492 Share on other sites More sharing options...
PaulRyan Posted May 21, 2011 Share Posted May 21, 2011 <?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. Quote Link to comment https://forums.phpfreaks.com/topic/237060-allowing-a-comma-in-the-price-field/#findComment-1218493 Share on other sites More sharing options...
dk4210 Posted May 21, 2011 Author Share Posted May 21, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/237060-allowing-a-comma-in-the-price-field/#findComment-1218494 Share on other sites More sharing options...
dk4210 Posted May 21, 2011 Author Share Posted May 21, 2011 Hi Paul, I want to remove the comma.. Not add one Quote Link to comment https://forums.phpfreaks.com/topic/237060-allowing-a-comma-in-the-price-field/#findComment-1218495 Share on other sites More sharing options...
Pikachu2000 Posted May 21, 2011 Share Posted May 21, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/237060-allowing-a-comma-in-the-price-field/#findComment-1218496 Share on other sites More sharing options...
dk4210 Posted May 21, 2011 Author Share Posted May 21, 2011 Same thing happens, it strips out the period also Quote Link to comment https://forums.phpfreaks.com/topic/237060-allowing-a-comma-in-the-price-field/#findComment-1218499 Share on other sites More sharing options...
PaulRyan Posted May 21, 2011 Share Posted May 21, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/237060-allowing-a-comma-in-the-price-field/#findComment-1218500 Share on other sites More sharing options...
Pikachu2000 Posted May 21, 2011 Share Posted May 21, 2011 Is a decimal such as 0.99 a valid value or not? Quote Link to comment https://forums.phpfreaks.com/topic/237060-allowing-a-comma-in-the-price-field/#findComment-1218504 Share on other sites More sharing options...
dk4210 Posted May 22, 2011 Author Share Posted May 22, 2011 O.99 would not be a valid number.. 10,000 - 800,000 would be valid numbers.. Quote Link to comment https://forums.phpfreaks.com/topic/237060-allowing-a-comma-in-the-price-field/#findComment-1218539 Share on other sites More sharing options...
PaulRyan Posted May 22, 2011 Share Posted May 22, 2011 <?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. Quote Link to comment https://forums.phpfreaks.com/topic/237060-allowing-a-comma-in-the-price-field/#findComment-1218551 Share on other sites More sharing options...
dk4210 Posted May 22, 2011 Author Share Posted May 22, 2011 I will give it a try Paul and let you know.. Thanks for the help Quote Link to comment https://forums.phpfreaks.com/topic/237060-allowing-a-comma-in-the-price-field/#findComment-1218565 Share on other sites More sharing options...
Pikachu2000 Posted May 22, 2011 Share Posted May 22, 2011 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; } } Quote Link to comment https://forums.phpfreaks.com/topic/237060-allowing-a-comma-in-the-price-field/#findComment-1218568 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.