简体   繁体   中英

How to turn on/off a field from parsley.js form validation

在此处输入图像描述 When validating an email field [name=bar] in a form #foo, I want to switch validation of that field on and off, in relation to a checkbox.

Reading the docs, github issues and stackoverflow answers, I thought setting data-parsley-required="false" and/or data-parsley-validate="false" would be enough.

But it turns out, that all other constraints, like email, min-lenght, max-length are still validated and the input field still validates to an error condition. I would prefer it to validate to success or not at all.

<form id="foo">
    <input name="bar"
           maxlength="40"
           value=""
           class="form-control"
           data-parsley-validate="false"
           data-parsley-required="false"
           type="email"
           data-parsley-minlength="5"
           data-parsley-trigger="input keyup change"
           data-parsley-error-message="something is wrong">
</form>

See https://jsfiddle.net/88obg0sj/9/

So how is it possible to turn off field validation in way, it can be re-activated again?

您应该调整excluded选项,例如,通过向其添加", [data-parsley-validate="false"]"

You can follow this way:-<\/strong>

//destroy parsley
$('form').parsley().destroy();

//set required attribute on input to false
$('input').attr('data-parsley-required', 'false');

//reinitialize parsley
$('form').parsley();

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