【问题标题】:Required Field Validator Issue必填字段验证器问题
【发布时间】:2014-09-17 15:18:33
【问题描述】:

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

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

请帮忙。

【问题讨论】:

    标签: c# javascript jquery


    【解决方案1】:

    “另外请找到我调用的 Datepicker 的 JS”,不确定您的意思。要触发onchange 事件,输入必须首先模糊。使用oninput 事件更准确。

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

    【讨论】:

    • 当我选择日期时,文本框不接受该值,因为必填字段验证器消息不会发送。
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-03-23
      • 1970-01-01
      • 1970-01-01
      • 2011-11-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多