必填字段验证器问题

时间:2014-09-16 15:43:17

标签: c# javascript jquery

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

<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>

另请找到我称之为Datepicker的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>

请帮忙。

2 个答案:

答案 0 :(得分:0)

&#34;另外请找到我称之为#34;的Datepicker的JS,不确定你的意思。要触发onchange事件,输入必须先模糊。使用oninput事件可获得更高的准确性。

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

答案 1 :(得分:0)

我尝试从文本框中删除水印,并按照我的要求接受了验证。

  <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>