[SOLVED] Format a field as currency

4 years 9 months ago - 4 years 9 months ago #313214 by ericmuc
Replied by ericmuc on topic Format a field as currency
Thanks, that would be great, if this improvement could be added soon!

Yes, I am on the way and found:

Currency numbers with optional dollar sign and thousand separators and optional 2 decimals ($1,000,00.00, 10000.12, 0.00):
^$?\-?([1-9]{1}[0-9]{0,2}(\,\d{3})*(\.\d{0,2})?|[1-9]{1}\d{0,}(\.\d{0,2})?|0(\.\d{0,2})?|(\.\d{1,2}))$|^\-?$?([1-9]{1}\d{0,2}(\,\d{3})*(\.\d{0,2})?|[1-9]{1}\d{0,}(\.\d{0,2})?|0(\.\d{0,2})?|(\.\d{1,2}))$|^\($?([1-9]{1}\d{0,2}(\,\d{3})*(\.\d{0,2})?|[1-9]{1}\d{0,}(\.\d{0,2})?|0(\.\d{0,2})?|(\.\d{1,2}))\)$

I would like to change it into (1.000.000, 10.000, 0)

If I add the string in the perl field in the back of the field with / at start and end it shows "false regular expression".

Or do you have a information, how I can quickly add the perl expression for (1.000.000, 10.000, 0) ?

Thanks, best regards
Eric

add:

at regexr.com I tried /^[1-9]{1,3}\.\d{3}\.\d{3}|[1-9]{1,3}\.\d{3}/ with success, but if I add this in CB, it doesn't work well:

OK should be
100.000.000
10.000.000
1.000.000
and so on
Not OK
100.00.000
etc.

This works in regexr.com, but in CB it is possible to add
435.43543543
2.324zzz

Sorry for having this problem, i hope, I make it right here, but so far I think at regexr.com my string is working.

Do you have a hint?

add

The same at /^[1-9]{1,3}\.\d{3},\d{2}/
here it should be ok with: 1.000,00, but it is also ok with 1.000.0000, or 1.000,92e4

Please Log in to join the conversation.

4 years 9 months ago #313217 by krileon
Replied by krileon on topic Format a field as currency
The REGEXP needs to be PHP and JS valid if you want it to work in the browser and server side at the same time. I've ran your supplied REGEXP through RegexBuddy and checked it against PHP 7 and JS (Edge) with the following results.

^$?\-?([1-9]{1}[0-9]{0,2}(\,\d{3})*(\.\d{0,2})?|[1-9]{1}\d{0,}(\.\d{0,2})?|0(\.\d{0,2})?|(\.\d{1,2}))$|^\-?$?([1-9]{1}\d{0,2}(\,\d{3})*(\.\d{0,2})?|[1-9]{1}\d{0,}(\.\d{0,2})?|0(\.\d{0,2})?|(\.\d{1,2}))$|^\($?([1-9]{1}\d{0,2}(\,\d{3})*(\.\d{0,2})?|[1-9]{1}\d{0,}(\.\d{0,2})?|0(\.\d{0,2})?|(\.\d{1,2}))\)$

This errors with "Quantifiers can only be used after a token that can be repeated" for PHP and JS. It's not valid. Whatever language it was written for won't work in PHP or JS.

at regexr.com I tried /^[1-9]{1,3}\.\d{3}\.\d{3}|[1-9]{1,3}\.\d{3}/ with success, but if I add this in CB, it doesn't work well:

This will only match your following example in PHP and JS.

1.000.000

It does not match the following.

100.000.000
10.000.000
100.00.000

This works in regexr.com , but in CB it is possible to add
435.43543543
2.324zzz

That's because the REGEXP isn't strict enough. The below will prevent that.

/^[1-9]{1,3}\.\d{3}\.\d{3}|[1-9]{1,3}\.\d{3}$/

You were missing the trailing $.

The same at /^[1-9]{1,3}\.\d{3},\d{2}/
here it should be ok with: 1.000,00, but it is also ok with 1.000.0000, or 1.000,92e4

Same issue as above. You're missing the trailing $. The below should work fine to prevent such issues.

/^[1-9]{1,3}\.\d{3},\d{2}$/

Please keep in mind we do not assist with custom coding. CB just passes the supplied REGEXP through the respective language functions. I recommend looking into purchasing RegexBuddy where you can live test and validate your REGEXP against multiple languages and is what I've used for years. I find the free online tools to be inaccurate.


Kyle (Krileon)
Community Builder Team Member
Before posting on forums: Read FAQ thoroughly + Read our Documentation + Search the forums
CB links: Documentation - Localization - CB Quickstart - CB Paid Subscriptions - Add-Ons - Forge
--
If you are a Professional, Developer, or CB Paid Subscriptions subscriber and have a support issue please always post in your respective support forums for best results!
--
If I've missed your support post with a delay of 3 days or greater and are a Professional, Developer, or CBSubs subscriber please send me a private message with your thread and will reply when possible!
--
Please note I am available Monday - Friday from 8:00 AM CST to 4:00 PM CST. I am away on weekends (Saturday and Sunday) and if I've missed your post on or before a weekend after business hours please wait for the next following business day (Monday) and will get to your issue as soon as possible, thank you.
--
My role here is to provide guidance and assistance. I cannot provide custom code for each custom requirement. Please do not inquire me about custom development.

Please Log in to join the conversation.

4 years 9 months ago - 4 years 9 months ago #313222 by ericmuc
Replied by ericmuc on topic Format a field as currency
Thanks for your help! I bought the recommended software, found there an example and adjusted it, so here are now the right strings for

1) German Currency without Cents
1.000.000 =
/^[1-9]{1,3}(?:\.[0-9]{3})*?$/

2) German Currency with Cents
1.000.000,00 =
/^[1-9]{1,3}(?:[,][0-9]{2}|(?:\.[0-9]{3})*(?:,[0-9]{2})?)$/

I am writing this here to have this info for all users. I hope, that all users share their code also here in this forum. That would help much.

Another idea is that Joomlapolis creates a collection of such strings and codes to help customers / users to more easily use of CB.

Thanks again for your good and quick help. I appreciate this much.
The following user(s) said Thank You: krileon

Please Log in to join the conversation.

Moderators: beatnantkrileon
Time to create page: 0.216 seconds

Facebook Twitter LinkedIn