必填字段验证器问题

Required Field Validator Issue

本文关键字:问题 验证 字段      更新时间:2023-09-26

我有一个文本框,上面应用了日期选择器。文本框是必填字段。问题是,即使在选择了日期之后,所需的字段验证器消息也不会继续。请参阅我的代码供您参考:

<asp:TextBox ID="txtdob" runat="server" CssClass="txtfld-popup1"></asp:TextBox>
<asp:RequiredFieldValidator ID="reqDOB" runat="server" ControlToValidate="txtdob" ErrorMessage="*"></asp:RequiredFieldValidator>
<cc1:TextBoxWatermarkExtender ID="TextBoxWatermarkExtender5" runat="server" TargetControlID="txtdob" WatermarkText="Enter your Date of Birth"></cc1:TextBoxWatermarkExtender>

此外,请找到日期选择器的JS,我称之为:

<script type="text/javascript">
    $(function () {
        $("#txtdob").datepicker({
            changeMonth: true,
            changeYear: true,
            dateFormat: 'MM dd, yy',
            minDate: "-58Y",
            maxDate: "-18Y",
            yearRange: "-58:-18",
            showOn: "button",
            buttonImage: "../images/cal.gif",
            buttonImageOnly: true,
            showOn: "both"
        });
        $("#txtdob").on('keydown', function (e) {
            e.preventDefault();
            e.stopImmediatePropagation();
            return false;
        });
        $("#txtdob").on('cut copy paste', function (e) {
            e.preventDefault();
            e.stopImmediatePropagation();
            return false;
        });
        $("#format").change(function () {
            $("#txtdob").datepicker("option", "dateFormat", $(this).val());
        });
        $("#btnSubmit").on("click", function () {
            $('#lblDOB').text("");
            var isValid = Page_ClientValidate("");
            if (isValid) {
                var dob = Date.parse($("#txtdob").val());
                if (isNaN(dob)) {
                    $('#lblDOB').text("Enter Proper Date of Birth.");
                    $('#lblDOB').css({ 'color': 'red' });
                    return false;
                }
            }
        });
    });
</script>

请帮忙。

"还请找到我调用的Datepicker的JS",不确定你的意思。要触发onchange事件,输入必须首先模糊。使用oninput事件可获得更高的准确性。

$("#txtdob").on('input', function (e) {
    e.preventDefault();
    e.stopImmediatePropagation();
    return false;
});

我尝试从文本框中删除水印,它根据我的要求接受了验证。

  <script type="text/javascript">
    $(function () {
        $("input[id$=txtdob]").datepicker({
            changeMonth: true,
            changeYear: true,
            dateFormat: 'MM dd, yy',
            minDate: "-58Y",
            maxDate: "+0D",
            yearRange: "-58:+0",
            showOn: "button",
            buttonImage: "../images/cal.gif",
            buttonImageOnly: true,
            showOn: "both"
        });
        $("#txtdob").on('keydown', function (e) {
            e.preventDefault();
            e.stopImmediatePropagation();
            return false;
        });
        $("#txtdob").on('cut copy paste', function (e) {
            e.preventDefault();
            e.stopImmediatePropagation();
            return false;
        });
        $("#format").change(function () {
            $("#txtdob").datepicker("option", "dateFormat", $(this).val());
        });
    });
</script>

此外,请参阅相关文本框的HTML:

<asp:TextBox ID="txtdob" runat="server" CssClass="txtfld-popup1"></asp:TextBox>
<asp:RequiredFieldValidator ID="reqDOB" runat="server" ControlToValidate="txtdob" ErrorMessage="Please enter yout date of birth"></asp:RequiredFieldValidator>