help with a currency regular expression please

View attachment 117975
Sorry, that went right over my head. I have no idea what to do with that. I you can explain what goes where I would be very happy!

I don't know how that add-on works, but I think it uses custom php callback just like custom user fields, so you can follow this tuts here it will get you starting.

https://xenforo.com/community/resources/using-php-callbacks-in-custom-fields-by-waindigo-guide.2979/

This one too:

https://xenforo.com/community/threads/custom-user-field-callback-validate-value.28284/
 
Again, that regex you've gotten to work has some bugs. It will allow a lot of extra characters to pass as currency symbols that aren't actually currency symbols.

Here's an updated version that will allow either a currency symbol or a percent sign, but not both. Untested, but it should work:

Code:
(?u)^(?>(?<c>\p{Sc})[1-9]\d{0,2}+(?:(?<gd>(?<gdc>,)|(?<gdp>\.)|\ )\d{3}+(?:(?&gd)\d{3}+)*+|\d*+))(?:(?(gdc)\.|(?(gdp),|[,.]))\d*+)?+(?(c)|%)$

If that doesn't work for some reason ("u" flag doesn't work, maybe?), you could try this, but it'll have some bugs with certain unusual characters:
Code:
^(?>(?<c>[£$€])[1-9]\d{0,2}+(?:(?<gd>(?<gdc>,)|(?<gdp>\.)|\ )\d{3}+(?:(?&gd)\d{3}+)*+|\d*+))(?:(?(gdc)\.|(?(gdp),|[,.]))\d*+)?+(?(c)|%)$
 
I don't know how that add-on works, but I think it uses custom php callback just like custom user fields, so you can follow this tuts here it will get you starting.

It works exactly like custom user fields, but this has just gone beyond what I can deal with.

I am not that tech so thanks anyway for trying to help.
 
Untested but this should work...
Code:
^(£|\$|€)(\d{1,3}(\,\d{3})*|(\d+))(\.\d{2})?$|^(\d{1,3}(\,\d{3})*|(\d+))(\.\d{2})?(%)$

EDIT: Now tested and works.
 
Last edited:
And this is why most sites which handle money will give you a dropdown of valid currencies and the amount you enter is just a number.

Often, you can massively simplify a regex by slight tweaks to your business logic and move the hard stuff outside the regex.
 
From what I can tell his site won't be handling money at all. It looks like these are simple 'For Sale' or 'Wanted' posts where the transactions will take place outside of the site.

So while it may not be the best solution, what he's doing will work for his purposes.
 
From what I can tell his site won't be handling money at all. It looks like these are simple 'For Sale' or 'Wanted' posts where the transactions will take place outside of the site.

So while it may not be the best solution, what he's doing will work for his purposes.

Yes, these are for sale ads. But we do handle money in that we take a voluntary commission, and so we have a required field when posting to say how much the donation will be, and it's very useful for people to either specify an actual amount if they want, or a percentage of sale price, which is better fro people if they took a best offer.
 
Code:
^(£|\$|€)(\d{1,3}(\,\d{3})*|(\d+))(\.\d{2})?$
Tested and works.

Requires the £, $, or € currency symbol.

Interesting, all those years ago that one did not work with the xf1 addon (Custom Thread Fields) but it does now work on the xf2 thread fileds. Whereas the one that I found di work on the xf1 addon does not work with xf2.
 
Top Bottom