0

I don't see anywhere in the documentation on http://jqueryvalidation.org/ how you can validate based off of a regex.

How do you validate against a regex using this library?

Also, can someone please give me a link to where this is documented as well, so I don't think I'm losing my mind? I know I did html attribute based regex validation in the past using a validation library (not just the pattern attribute alone, because I also had a data-msg to display a custom validation message upon failing the regex)

toddmo
  • 20,682
  • 14
  • 97
  • 107
  • I kinda see this as a documentation failure of jquery validate. You go to features or getting started in the documentation and you don't see any mention of either 1. using html attributes and 2. additional methods including regex. If it's there in the documentation, it's kinda buried and kinda well hidden. – toddmo Mar 24 '18 at 19:03

2 Answers2

0

You need to first add this to scripts:

<script defer src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.17.0/additional-methods.min.js"></script>

Then you can do:

                    <input 
                        type="text" 
                        class="form-control" 
                        id="title" 
                        name="title" 
                        placeholder="Title"
                        required
                        pattern="^(lala)+(.+)$"
                        data-msg-pattern="Must start with at least one lala">
toddmo
  • 20,682
  • 14
  • 97
  • 107
0

The accepted answer, while it apparently works, does not actually answer the question. An example of using regex with the jqueryvalidation plugin has already been provided in an answer at How to validate email...

GDP
  • 8,109
  • 6
  • 45
  • 82