简体   繁体   中英

Parsley add pattern constraint?

How can I bind validation using the pattern attribute on an input to an existing bound parsley validator?

Let's say I have a form, which is already initialized, and the user actions something which will push in a diving license field to the form, this field needs patterns on it so that they only enter numbers.

FIELD:

<input id="74" name="DivingLicence" type="text" value="" pattern="\d+" data-parsley-pattern="\d+" data-error-message="The Diving License is not valid" >

Javascript:

var item = {
    field : '#74' 
}

$('form').parsley('addItem', item.field);
var field = $(item.field);
if ('string' === typeof field.attr('pattern')) {
    field.parsley('addConstraint', {
        'pattern' :  field.attr('pattern')
    });
}

This is what I've tried, but I can't seem to raise an error at all when I enter anything wrong....

It's quite unclear what is going on, or how you figured your javascript would work.

You shouldn't need any javascript (besides the one to add the <input> to your form), nor a data-parsley-pattern since parsley will look at the pattern attribute.

As usual, a CodePen would help, you can start from the Parsley examples.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM