Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use the parsley.js pattern tag?

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.

like image 954
KSPR Avatar asked May 05 '14 13:05

KSPR


People also ask

How to use parsley javascript?

Parsley's default DOM API is data-parsley- . and you just need to use it's property, property means email, number, date, digit etc, suppose if you want to validate email input, then you just have to add data-parsley-type="email" data-attribute in the same input element, that's it.

What is parsley CSS?

Parsley is a javascript form validation library. It helps you provide your users with feedback on their form submission before sending it to your server. It saves you bandwidth, server load and it saves time for your users.

How do you remove parsley validation?

You can deactivate parsley validation, remove the required attribute for the element that you want to disable validation for, and then reactivate Parsley. First, let's assume you bound Parsley validation to your form like so: $('#my-form'). parsley({ //options });


1 Answers

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

like image 59
guillaumepotier Avatar answered Sep 19 '22 15:09

guillaumepotier