简体   繁体   English

formvalidation.io表单验证。 如果“其他”,请解释

[英]formvalidation.io form validation. If “other”, please explain

I'm using the FormValidation plugin in Bootstrap and am loving it, except I can't figure out one thing. 我在Bootstrap中使用FormValidation插件并且很喜欢它,但我无法确定一件事。 I have a form with an "other" check box. 我有一个带有“其他”复选框的表单。 If you check "other", I want an explanation typed into another field. 如果您选中“其他”,我想要在另一个字段中输入解释。 I've been all over the website (formvalidation.io), but I'm not finding an answer. 我一直在网站上(formvalidation.io),但我找不到答案。 Unfortunately, I know just enough JS to be dangerous. 不幸的是,我知道JS足够危险。

Here's the code for the validator I'm using on the check boxes. 这是我在复选框上使用的验证器的代码。

'work_type[]': {
    validators: {
    choice: {
        min: 1,
        message: 'Please choose at least one work type.'
    }
    }
}

There's another field called "other_work" that I would like to require a string in if work_type has a value of "Other Work". 如果work_type的值为“ Other Work”,那么我还需要一个名为“ other_work”的字段。

For anyone trying to figure this out check out callback functions 对于任何想弄清楚这一点的人,请查看回调函数

You declare your own function something like this: 你声明自己的函数是这样的:

function optionOtherValidator(value) {         
    if ($('#amountOther input').is(':checked');) {
        return /^\w+$/.test(value);
    }

    return true;
},

Then in your fields array you declare it as a callback: 然后在您的fields数组中,将其声明为回调:

fields: {
    optionOther: {
        validators: {
            callback: {
                message: 'Please specify a value for other',
                callback: optionOtherValidator
            }
        }
    }
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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