简体   繁体   中英

Required Field Validator Issue

I have a textbox on which I have applied a datepicker. And the textbox is required field. The problem is that, even after selecting the date the required field validator message is not going. Please see my code for your reference:

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

Also please find the JS for the Datepicker which I called:

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

Please help.

"Also please find the JS for the Datepicker which I called", not sure what you mean with that. To trigger onchange event the input must blur first. Use oninput event for more accuracy.

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

I tried with removing the watermark from the textbox and it accepted the validations as per my requirement.

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

Also Please see the HTML for the related textbox:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM