簡體   English   中英

必填字段驗證器問題

[英]Required Field Validator Issue

我在一個文本框上應用了日期選擇器。 並且文本框為必填字段。 問題是,即使選擇了日期之后,所需的字段驗證器消息也不會顯示。 請參閱我的代碼以供參考:

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

請幫忙。

“也請找到我調用的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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM