Closed MikeSteigerwald closed 6 years ago
Do you know which elements and fields contain numbers beforehand? If yes, you can just add a class to them ("currency", for example), and then let the formatAll function only format elements that match that class by passing in a selector. See example 4 in the docs :)
Yes, I do know which elements and fields contain numbers beforehand. Unfortunately, I occasionally miss something in validation which lets the user enter a non-numeric value. If that makes it all the way into formatAll, I get NaN results which I then confuse my users with. I've added the isNan checking to 'abort' the formatting if the actual input is not numeric.
The other change I've made is to apply the formatting to input elements, i.e., if the element has a value property, format it just like we do for the textContent property.
Apologies for the delay in replying. I think we might just add this into the library itself. I.e. if the value is non-numeric, replace it with a user-defined value. We'll look to release an update in the next few weeks, so look out for it in there :)
Superb! My gratitude for this package precludes any need to apologize for delays!
I believe the functionality you require has been added in the latest version (2.1.0):
OSREC.CurrencyFormatter.format('10', { currency: 'USD', valueOnError: 'Woops!' }); // Returns $10.00
OSREC.CurrencyFormatter.format('abcd', { currency: 'USD', valueOnError: 'Woops!' }); // Returns Woops!
I hesitate to even call this an issue, since I'm just asking for advice. For our app, I expanded the matches[ i ] assignment in formatAll to make sure the existing string was a number (otherwise, I get errors) and to make sure it got applied to input elements (i.e., has value, not textContent)
` for (var i = 0; i < matches.length; ++i) {
My two questions: