We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 11449
    • 28 Posts
    I'm having no luck with the Regular Expression Validator on a Template Variable of the Text input type. I have used examples strait off of http://www.regexr.com/ to test them after I couldn't get my own ones to work and nothing. It always gives me the error, even with a string that validates on regexr.com and other regular expresion testers. Is this functionality broken? Do I have to write it diferently?

    I have tried writing the expression with and without the leading and trailing /, with and without a g, for example:

    \b\d{3}[-.]?\d{3}[-.]?\d{4}\b
    /\b\d{3}[-.]?\d{3}[-.]?\d{4}\b/
    /\b\d{3}[-.]?\d{3}[-.]?\d{4}\b/g
      • 3749
      • 24,544 Posts
      Give an example of what you would consider a valid string.

      You need to escape the - inside a character class. [\-.]
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 11449
        • 28 Posts
        Ok so what I would consider a valid string would be:

        149-567-8543

        I have tried escaping the dash "-" as you mentioned and none of the following work. I must be doing something wrong.

        \b\d{3}[/-.]?\d{3}[/-.]?\d{4}\b
        /\b\d{3}[/-.]?\d{3}[/-.]?\d{4}\b/
        /\b\d{3}[/-.]?\d{3}[/-.]?\d{4}\b/g
          • 3749
          • 24,544 Posts
          Testing in PhpStorm, none of those worked for me, but this did. The the / in your character class isn't escaping the hyphen (that would be \-), so the hyphen is defining a range. If you put the hyphen at the end of the character class, it doesn't need escaping.

          \b\d{3}[.-]\d{3}[.-]\d{4}\b


          The regexp code doesn't add a delimiter, so you'd have to add a delimiter to each end like this:

          /\b\d{3}[.-]\d{3}[.-]\d{4}\b/


          or

          @\b\d{3}[.-]\d{3}[.-]\d{4}\b@


          or
          %\b\d{3}[.-]\d{3}[.-]\d{4}\b%


          The code uses preg_match(), which doesn't take the /g modifier, so that will always cause an error. Withoug it, you'll only get the first match unless you write your own custom validator that uses preg_match_all().
            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting
            • 11449
            • 28 Posts
            Thanks for the explination and looking into this. I tried all of your examples the one without the delimiter as well as the 3 different kinds of delimiters and it still throws the validation error.

            I also tried my previous example but with the correct escaping character [\-.] for the character class and that didn't work ether.

            /\b\d{3}[\-.]?\d{3}[\-.]?\d{4}\b/


            This obviously isn't anything I have knowledge about but I'm wondering if something is broken with MODX TV RegEx validation.
              • 3749
              • 24,544 Posts
              It's quite possible. Almost no one uses it. I didn't have time to actually test it.
                Did I help you? Buy me a beer
                Get my Book: MODX:The Official Guide
                MODX info for everyone: http://bobsguides.com/modx.html
                My MODX Extras
                Bob's Guides is now hosted at A2 MODX Hosting
                • 51741
                • 1 Posts
                Hi, you can use the following regexp:
                [0-9]{3}\-[0-9]{3}\-[0-9]{4}

                It works well.
                  • 13428 ☆ A M B ☆
                  • 1,031 Posts
                  There are some differences between javascript and pcre (php) regular expressions.

                  On the page https://regex101.com/#javascript you could generate and test your regular expressions. After you have generated a valid expression, click on 'code generator' and the tool will escape the expression for you. Copy the properly escaped code of the tool without the delimiters to the regular expression field in MODX.

                  In your case it should be
                  \\b\\d{3}[\\-.]?\\d{3}[\\-.]?\\d{4}\\b
                    • 42766
                    • 47 Posts
                    Hi Jako - you mention there's a difference between the Javascript and (PCRE) PHP regex patters, but don't mention which one is used by the TVs - could you please elaborate if you know?
                      • 13428 ☆ A M B ☆
                      • 1,031 Posts
                      Should be JavaScript, if the validation is done by ExtJS (not sure about this).