简体   繁体   English

如果文本框输入值发生变化,请验证该字段……但如果……

[英]If text box input value changes, validate the field… but if… Javascript

I am trying to validate a few fields using the formvalidator from formvalidation.io but am running into a few problems using it. 我正在尝试使用来自formvalidation.io的formvalidator验证一些字段,但是在使用它时遇到了一些问题。 I need to find out if a textbox (date,telephone,email) has a value (user filled the textbox out) and validate the field. 我需要找出一个文本框(日期,电话,电子邮件)是否具有值(用户将文本框填满)并验证该字段。 But if the user does not fill out the textbox, do not validate the field. 但是,如果用户未填写文本框,请不要验证该字段。 A user may start to fill out the textbox and then delete its contents and if they do this, the field should not be validated. 用户可能会开始填写文本框,然后删除其内容,如果这样做,则不应验证该字段。 Can anyone help me out with this? 谁能帮我这个忙吗?

I have tried a few different techniques such as addEventListener and such but I haven't gotten it exactly how I need it. 我尝试了几种不同的技术,例如addEventListener等,但是我没有得到我真正需要的技术。

Here is an example of what I currently have. 这是我目前拥有的一个例子。

   $("#Q_dob_text").change(function () {
    var dob = document.getElementById('Q_dob_text').value;
    if (dob !== '') {
        $('#registry').formValidation({
            framework: 'bootstrap',
            icon: {
                valid: 'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            },
            fields: {
                dob: {
                    validators: {
                        date: {
                            format: 'MM/DD/YYYY',
                            message: 'The value is not a valid date'
                        }
                    }
                }
            }
        });
    }
});

And I have also tried variations of this too... 我也尝试过这种变化...

 document.getElementById('Q_date_text').addEventListener("keydown", dateFx);
    function dateFx() {
        var date = document.getElementById('Q_date_text');
        if (date !== '') {
            $('#registry').formValidation({
                framework: 'bootstrap',
                icon: {
                    valid: 'glyphicon glyphicon-ok',
                    invalid: 'glyphicon glyphicon-remove',
                    validating: 'glyphicon glyphicon-refresh'
                },
                fields: {
                    date: {
                        validators: {
                            date: {
                                format: 'MM/DD/YYYY',
                                message: 'The value is not a valid date'
                            }
                        }
                    }
                }
            });
        }
    }

Any help is greatly appreciated! 任何帮助是极大的赞赏!

What's going wrong is that the .formValidation call initializes (binds) the plugin to always validate, so as soon as you've run that code once, it will always keep working (even though a user removes the entered text). 出问题的是, .formValidation调用初始化(绑定)插件以始终对其进行验证,因此,一旦您运行该代码一次,它将始终保持工作状态(即使用户删除了输入的文本)。

What you really want to do is initialize it once, and update it's validation settings (in the validators setting) to make it not validate the dob (from your first example) if it's empty. 您真正想做的是将其初始化一次,并更新它的验证设置(在validators设置中),以使其在空的情况下(从第一个示例中)不验证dob You could do so by calling "updateOption" or "removeField", see the documentation . 您可以通过调用“ updateOption”或“ removeField”来实现,请参见文档

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

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