How to use the parsley.js pattern tag?
Asked Answered
C

1

14

How do I add the attr:data-parsley-pattern="" tag correctly to a input?

I wrote this:

<input type="text" name="serialnr" id="serialnr" data-parsley-pattern="^[a-zA-Z]{4}[ -]?[a-zA-Z]{4}[ -]?[a-zA-Z]{4}[ -]?[a-zA-Z]{4}$">

But this does exactly nothing (No validation takes place at all while the other input are validated correctly). Do I have to add data-parsley-required="true"? (Which I did but that didn't help). I tested the regex with https://www.debuggex.com/ so It shouldn't be a typo. What do I miss?

I call parsley only via data-attributes. Does that matter maybe?

Thank you.

Camp answered 5/5, 2014 at 13:27 Comment(1)
All looks good, do you mind including the whole form (or at least the form tag and other inputs) or whipping up a quick JSFiddle?Serin
B
14

Your code seems to work perfectly here: http://jsfiddle.net/c2r4R/

What you need to know: Parsley validates only empty required fields. Your data-parsley-pattern directive is not applied if field is left empty. You'll need to add a required tag in order to throw an error if field is left empty, and throw another pattern error if field do not match your serial number pattern.

Best

Burnsed answered 10/5, 2014 at 9:29 Comment(2)
Thanks I found the error, my cms form plugin stripped the whole regex tag :( but hardcoded it works.Camp
Is there any solution to change the default message using parsley?Upmost

© 2022 - 2024 — McMap. All rights reserved.