We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 23946
    • 4 Posts
    Hi, I have eForm working well but want to add some validation to block users submitting forms that contain a specific string in the message text. Looking at the documentation looks like I could use the #REGEX feature to do this. Is this the best way? My regular expression knowledge is a bit limited.

    For example I know \abcd\ will test if the string contains abcd but how do I say ’does not contain’ abcd?

    Am I on the right track?

    Thanks in advance.

    Tim
      • 30223
      • 1,010 Posts
      Why don’t you just filter out the words you don’t want with #FILTER?

      from eform documentation...
      # #FILTER - Filters do not validate the input but simply replace words or values using filter criteria. You can use the following filters:

      * #FILTER #LIST
      use double pipe to separate 2 comma separated lists of words and replacement values.
      example; #FILTER #LIST badword,verybadword||goodword,verygoodword
      * #FILTER #EVAL
      example: #FILTER #EVAL return myFilterFunction($value);
      (offcourse you have to make sure the function exists somehow)

      example filter function
      function myFilterFunction($value){
      $badWords = array(’scribble’,’coding’);
      $goodWords = array(’design’,’sleep’);
      return str_replace($badWords,$goodWords,$value);
      }
      * #FILTER #REGEX
      regular expression replace - syntax as for preg_replace() separate the search and replacement expression with a double pipe symbol (||)
      TODO: example

        • 23946
        • 4 Posts
        Thanks for the suggestion. I want to validate the input though. If the message contains the string I want to reject the form and display an error text. eForm is great for this. For example I can make a validation rule #REGEX /abcd/ which rejects the field if it DOES NOT contains the string abcd but what I really want is to reject it if it DOES contain the string.

        Any regular expression experts out there who can advise if this is possible? Or maybe I should be using the eForm #EVAL feature?
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          Use a negative regexp; that’s a ^ to negate an expression.

          * #REGEX - regular expression - syntax as for preg_match() - see php manual
              example: #REGEX /^[a-z]+ [a-z0-9_]+/i
          The above example will look for anything that is NOT alphanumeric, case insensitive. er... I think... probably best to go over the php manual on the subject!
            Studying MODX in the desert - http://sottwell.com
            Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
            Join the Slack Community - http://modx.org
            • 30223
            • 1,010 Posts
            In addition there’s a handy little tool for testing regular expressions on a windows platform: http://www.weitz.de/regex-coach/
              • 33968
              • 863 Posts
              Probably too late for the original poster, but I came across this same issue and got it to work like this:

              #REGEX /[^abcd]/


              This rejects the form submission when the field value is equal to ’abcd’.

              I’ve used it to check that the user has updated form fields that already contain a default value/label, such as ’first name’, ’surname’, etc...