We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 37143
    • 74 Posts
    Hi, I am having some trouble with the setup for a regexp using latest Form It. I am trying to just validate that the first character of a password field is a letter, and its a min of 6 characters.

    &validate=`password:required:minLength=^6^:regexp=^/[a-zA-Z]{1}/^`

    I also have setup the custom error messages:
    &password.vTextMinLength=`Please enter a password of at least 6 characters.`
    &password.vTextRegexp=`Please begin your password with a letter from A-Z uppercase or lowercase`

    I am not getting any errors in the modx error log, but I can't seem to get the regexp validation working. I don't get that error message regardless of what I enter, but the min character validator works properly.

    Anyone have any experience with this? What is wrong with my syntax?

    Thanks!

    Anthony
      • 3749
      • 24,544 Posts
      You haven't anchored your regex to the beginning of the input, so it will accept any string that contains [a-zA-Z]. Unfortunately, the solution uses the ^ character, which will break the FormIt tag. This regex would handle both the first character requirement and the min length:

      /^[a-zA-Z]\w{5,}/


      but the FormIt will interpret the ^ character as a delimiter for the property rather than part of the regex.

      I don't see any way out except for a custom validator (untested):

      &validate=`password:checkPassword`


      /* checkPassword snippet */
      
      $success = true;
      $value = trim($value);
      if (strlen($value) < 6) {
          $success = false;
          $validator->addError($key, 'Must be at least 6 characters long!');
      }
      
      if (!preg_match('/^[a-z]/i', $value)) {
          $success = false;
          $validator->addError($key, 'Must start with a letter!');
      }
      
      return $success;
      



      The good news it that as long as you're doing a custom validator, you could also test password strength.
      [ed. note: BobRay last edited this post 11 years, 6 months ago.]
        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
        • 37143
        • 74 Posts
        Bob, thank you so much for the help! It is extremely appreciated!
        Worked fine!

        Regards,
        Anthony
          • 3749
          • 24,544 Posts
          I'm glad it worked for you. smiley
            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