2 Replies Latest reply on Mar 4, 2018 1:53 PM by Frank Breen

    Form validation for multiple fields

    Frank Breen

      On occasion people fill out our forms with bad data, for example:

       

      First Name: aaa

      Last Name: aaa

      Email something@gmail.com

      Company Name: aaa

       

      I found this example written by Sandford Whiteman: https://codepen.io/figureone/pen/JKzjvE, but it's only works for 1 field at a time, attached is my edited code. What edits do I need to make to get this to work on FirstName, LastName, Company and also stop Freemium email addresses? I've tried combining the examples out on the nation but am getting code conflicts or the form submits without checking my rules.

        • Re: Form validation for multiple fields
          Sanford Whiteman

          I'm this Sanford Whiteman actually.

           

          but it's only works for 1 field at a time

          Not at all! It's specifically written to be extensible, that's why it says "array of validator objects."

           

          If you wanted to validate both LastName and Website, for example, you'd use:

           

            var extendedValidators = [
            {
              field: 'LastName',
              pattern: /[0-9]/,
              predicate: false,
              message: 'Last names can\'t have numbers.',
            },
            {
              field: 'Website',
              pattern: /\.example\.com$/i,
              predicate: false,
              message: 'Websites can\'t be example.com sites.',
            }
            ];
          

           

          Of course this assumes you can express your rules as regular expressions, and not everything fits a regex.  You might want to pass a callback function, which this particular version of the code does not support (I think there's a another demo that does, have to look for it).

           

          In your case, if you want to catch the same character repeated 3 or more times, that's the regex

           

          /(\w)\1{2,}/
          

           

          But I'd be extremely wary of false positives.  "AAA" is one of the most-well known brands in America, and lots of of small companies prepend "AAA" to their name. For personal names I guess it's pretty sound, but it's hardly the only way to come up with a fake name.

          1 of 1 people found this helpful