1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

help with a currency regular expression please

Discussion in 'General PHP and MySQL Discussions' started by Mr Lucky, Aug 29, 2015.

  1. Mr Lucky

    Mr Lucky Well-Known Member

    What I want is to match the value so a user must put in a currency symbol and amount

    I can do this with a specific currency, e.g.

    for £

    However I want to allow $ and € as well (or ideally any currency symbol)

    Can someone please help with this?

  2. Brogan

    Brogan XenForo Moderator Staff Member

  3. Mr Lucky

    Mr Lucky Well-Known Member

  4. Mr Lucky

    Mr Lucky Well-Known Member

  5. Jake B.

    Jake B. Well-Known Member

    Mind providing the code you're using? I just did a quick dirty test and it seemed to work fine for any instance except using commas as the decimal separator
  6. Mr Lucky

    Mr Lucky Well-Known Member

    Thanks Jake

    I'm trying the exact code Brogan suggested

    It tests fine with Regex101 - online regex editor and debugger

    But using it on my forum with custom fields it only allows the $ sign.
  7. Zenexer

    Zenexer Active Member

    \p{Sc} will match any currency symbol if you have Unicode support enabled (/.../u or /(?u).../). Your other examples won't work without Unicode support because they're multi-byte in UTF-8; only $ will match properly. Others will end up matching a lot more characters than you're intending to match.

    Here's an advanced locale-independent regex. Keep in mind that many locales swap the usage of "," and ".". Requires modifiers x and u.
      (?<currency_symbol> \p{Sc} )
      (?<leading_group> [1-9] \d{0,2}+ )
      (?<group> \d{3}+ )
      (?<non_leading_groups> (?&group) (?:(?&group_delim)(?&group))*+ )
        (?# Use the opposite of group_delim. )
          | (?(group_delim_period)
            | [,.] (?# There's no definitive grouping_delim. )
          (?<group_delim_comma> , ) |
          (?<group_delim_period> \. ) |
          \  (?# Allow whitespace as a delimiter. )
        | \d*+ (?# It's also possible that there's no delimiter. )
    I've posted example test cases at https://regex101.com/r/wF8aJ6/.

    In case you're one of those crazy people who likes to compress complex regexes into unreadable gibberish, here's the same thing in one line. Good luck reading it. ;)
    /^(?>\p{Sc}[1-9]\d{0,2}+(?:(?<gd>(?<gdc>,)|(?<gdp>\.)|\ )\d{3}+(?:(?&gd)\d{3}+)*+|\d*+))(?:(?(gdc)\.|(?(gdp),|[,.]))\d*+)?+$/u
    That one doesn't require modifier x, but I escaped the space in case anyone reading this accidentally uses it that modifier on it.
    Last edited: Sep 26, 2015
  8. Mr Lucky

    Mr Lucky Well-Known Member

    Tnaks, but that one doesn't seem to work either. The examples work at regex101, but not when I use for a match in a custom field on xenforo.
  9. Snog

    Snog Well-Known Member

    Tested and works.

    Requires the £, $, or € currency symbol.
    Last edited: Sep 26, 2015
  10. Mr Lucky

    Mr Lucky Well-Known Member

    Weird that it doesn't work on my forum:

    Screen Shot 2015-09-26 at 18.49.55.png
  11. Snog

    Snog Well-Known Member

    Is this in a custom user field or a custom field in resource manager? It works in both places for me.

    If not, how are you checking the regex?

    EDIT: It also works in my Advanced Application Forms system.
    Last edited: Sep 26, 2015
  12. Mr Lucky

    Mr Lucky Well-Known Member

  13. Snog

    Snog Well-Known Member

    I would suggest posting in that add-ons thread then.

    If you try that regex in an XF custom user field (without that add-on installed), it works.
  14. Cyb3r

    Cyb3r Well-Known Member

    A suggestion, it's hard for members to insert the currency symbol I suggest adding another drop down field with the currency prefix like GBP, USD and EUR.
  15. Zenexer

    Zenexer Active Member

    You need to make sure the correct modifiers are applied. Try it like this:
    (?u)^(?>\p{Sc}[1-9]\d{0,2}+(?:(?<gd>(?<gdc>,)|(?<gdp>\.)|\ )\d{3}+(?:(?&gd)\d{3}+)*+|\d*+))(?:(?(gdc)\.|(?(gdp),|[,.]))\d*+)?+$
  16. Mr Lucky

    Mr Lucky Well-Known Member

    OK, I think I know the reason is it took a while for the cache on the site . So it is now working, thank you very much for that.

    I have one more question about this.

    How would a make an expression that requires either the £,$ € symbol OR a percentage?

    So £20, €20, $20, 20% are all OK, but not 20.

  17. Cyb3r

    Cyb3r Well-Known Member

    This should require a symbol, I don't know what do you mean by requiring percentage?

    Edit: Now I see what you mean, you have to add another regex match for the percentage like this:
    So your PHP code should look like this:
    $pattern1 '/^(£|\$|€){1}(\d{1,3}(\,\d{3})*|(\d+))(\.\d{2})?$/';
    $pattern2 '/^(\d{1,3}(\,\d{3})*|(\d+))(\.\d{2})?(%){1}$/';

    preg_match($pattern1$price) || preg_match($pattern2$price))
    // Your code
    Last edited: Sep 28, 2015
  18. Mr Lucky

    Mr Lucky Well-Known Member

    How would I have two reggae matches for one field? I didn't know that as possible, so I wonder if it is possible to have one expression that covers currency symbols and the % sign?

  19. Cyb3r

    Cyb3r Well-Known Member

    Check my reply again, I have explained it with a PHP code.
  20. Mr Lucky

    Mr Lucky Well-Known Member

    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!

    Screen Shot 2015-09-28 at 19.44.08.png

Share This Page