How to use the parsley.js pattern tag?

我们两清 提交于 2019-12-05 12:44:17

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/23473710/how-to-use-the-parsley-js-pattern-tag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!