How to strip commas from number input fields?

arcadeicons

Member
I have an element designed to accept scores, and I run php validations against the scores. If there is a comma in the score entered it breaks the validation script. I have the field format set to integer and number format to yes.

Some users will use commas when entering their scores so I have to be able to accommodate that.

my validation code.

PHP:
// get score
$gameScore = $formModel->formData[submit_score___gamescore];
// get joins, they'll probably be arrays, so have to check for that
$tournID = $formModel->formData[submit_score___tournID_raw];
$tournID = is_array($tournID) ? $tournID[0] : $tournID;
$eventID = $formModel->formData[submit_score___eventID_raw];
$eventID = is_array($eventID) ? $eventID[0] : $eventID;
$gameID = $formModel->formData[submit_score___gameID_raw];
$gameID = is_array($gameID) ? $gameID[0] : $gameID;
$locID = $formModel->formData[submit_score___collectiontype_raw];
$locID = is_array($locID) ? $locID[0] : $locID;
$playerID = $formModel->formData[submit_score___userID_raw];
$playerID = is_array($playerID) ? $playerID[0] : $playerID;
// build a query to find any existing rows with higher score
$myDb = JFactory::getDbo();
$myQuery = $myDb->getQuery(true);
$myQuery
   ->select('*')
   ->from('submit_score')
   ->where('tournID = ' . $myDb->quote($tournID))
   ->where('eventID = ' . $myDb->quote($eventID))
   ->where('gameID = ' . $myDb->quote($gameID))
   ->where('collectiontype = ' . $myDb->quote($locID))
   ->where('userID = ' . $myDb->quote($playerID))
   ->where('gamescore >= ' . $gameScore);

$myDb->setQuery($myQuery);
$scores = $myDb->loadObjectList();

// returns false if we found any rows
return empty($scores);
 
$gameScore = $formModel->formData[submit_score___gamescore];
$gameScore = preg_replace('/[^\d.]/','', $gameScore);

Should strip everything except numbers and decimal point. Remove the decimal point from the expression if you don't need that.

Or if you really only want to strip commas ...

$gameScore = str_replace(',', '', $gameScore);

We do have an unNumberFormat() method in the element model, which takes your specific number format (if enabled) and un-applies it (as we have exactly the same problem you do with other aspects of handling formatted numbers). But that's probably overkill if you just want to strip commas from a number. Also I think it might fail if the number isn't formatted. Or not. Who knows, it's Monday.

-- hugh
 
We are in need of some funding.
More details.

Thank you.

Members online

No members online now.
Back
Top