jQuery datepicker 验证消息问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2287011/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 13:10:36  来源:igfitidea点击:

jQuery datepicker validation message issue

jqueryjquery-validatejquery-ui-datepicker

提问by Abhishek

I'm using the jquery datepicker plugin at http://plugins.jquery.com/project/datepickwith the datepicker validation plugin.

我在http://plugins.jquery.com/project/datepick上使用 jquery datepicker 插件和 datepicker 验证插件。

<script id="frmValidation" type="text/javascript">

$(document).ready(function(){
    var validator = $("#frmTest").validate({
        rules:{
            fname: "required",
            dobPicker: "required" 
        },
        messages:{
            fname: "Please enter a name",
            dobPicker: "Select a date"
        },

    });
    $('#dobPicker').datepick();
    $.datepick.setDefaults({showOn: 'both', dateFormat: 'dd-mm-yy', yearRange:'1900:2010'});
});
</script>

And the body of the document is as follows :

文件正文如下:

<form id="frmTest" action="" method="post">
<div id="error-list"></div>
<div class="form-row">
<span class="label"><label for="fname">Name</label></span>
<input type="text" name="fname" />
</div>
<div class="form-row">
<span class="label"><label for="dobPicker">DOB</label></span>
<input type="text" id="dobPicker" name="dobPicker" style="margin-left: 4px;"/>
</div>
<div class="form-row">
<input type="submit" name="submit" value="submit"/>
</div>
</form>

The form validates the first time but the error message for the datepicker does not disappear immediately a date is selected.. however it goes away if the date is selected the second time. Any help to make it go the first time a date is selected will be appreciated

该表单第一次验证,但日期选择器的错误消息不会在选择日期后立即消失......但是如果第二次选择日期,它就会消失。任何帮助第一次选择日期的帮助将不胜感激

回答by Andrew Roazen

You want to set this in your datepicker defaults:

你想在你的日期选择器默认值中设置它:

onClose: function() {$(this).valid();},

As soon as another date is selected it will make the validator plugin recheck the field.

一旦选择了另一个日期,验证器插件就会重新检查该字段。

回答by YeeHaw1234

The "best answer" didn't work for me. I figured out that the validator was validating the date format even though I didn't ask it to. Here's my solution:

“最佳答案”对我不起作用。我发现验证器正在验证日期格式,即使我没有要求它。这是我的解决方案:

$('form').validate(
    rules:
    {
        MyDateControl:{
            required:true,
            date:false
        }
    }
);