• Hello Fabrik Community

    Fabrik is now in the hands of the development team that brought you Fabrik for Joomla 4. We have recently transitioned the Fabrik site over to a new server and are busy trying to clean it up. We have upgraded the site to Joomla 4 and are running the latest version of Fabrik 4. We have also upgraded the Xenforo forum software to the latest version. Many of the widgets you might have been used to on the forum are no longer operational, many abandoned by the developers. We hope to bring back some of the important ones as we have time.

    Exciting times to be sure.

    The Fabrik 4.0 Official release is now available. In addition, the Fabrik codebase is now available in a public repository. See the notices about these in the announcements section

    We wish to shout out a very big Thank You to all of you who have made donations. They have really helped. But we can always use more...wink..wink..

    Also a big Thank You to those of you who have been assisting others in the forum. This takes a very big burden off of us as we work on bugs, the website and the future of Fabrik.

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

Back
Top