Set ASP.Net CustomValidator ErrorMessage on TextBox Change using JavaScript

Last Reply 2 months ago By pandeyism

Posted 2 months ago

hello,

i am using this solution it works fine but i want to fire validation on textbox blur event not on button click event please advice

<script type="text/javascript">
    function ValidateDOB(sender, args) {
        var dateString = document.getElementById(sender.controltovalidate).value;
        var regex = /(((0|1)[0-9]|2[0-9]|3[0-1])\/(0[1-9]|1[0-2])\/((19|20)\d\d))$/;
        if (regex.test(dateString)) {
            var parts = dateString.split("/");
            var dt = new Date(parts[1] + "/" + parts[0] + "/" + parts[2]);
            var dtCurrent = new Date();
            dtCurrent.setMonth(dtCurrent.getMonth() - 6);
            if (dt.getFullYear() <= dtCurrent.getFullYear()) {
                if (dt > dtCurrent) {
                    args.IsValid = true;
                } else {
                    args.IsValid = false;
                }
            }
        } else {
            sender.innerHTML = "Enter date in dd/MM/yyyy format ONLY."
            args.IsValid = false;
        }
    }
</script>
<asp:TextBox ID="txtDate" runat="server" Text="02/07/2018" />
<asp:CustomValidator ID="cvDate" runat="server" ClientValidationFunction="ValidateDOB"
    ControlToValidate="txtDate" ErrorMessage="Candidate has not worked for 6 months."
    ValidationGroup="gp1" Display="Dynamic" ForeColor="Red" />
<br />
<br />
<asp:Button ID="btnValidate" Text="Validate" runat="server" ValidationGroup="gp1" />

 

You are viewing reply posted by: pandeyism 2 months ago.
Posted 2 months ago

Hey nauna,

Please refer below sample.

HTML

<div>
    <script type="text/javascript">
        function ValidateDOB(sender, args) {
            var dateString = document.getElementById(sender.controltovalidate).value;
            var regex = /(((0|1)[0-9]|2[0-9]|3[0-1])\/(0[1-9]|1[0-2])\/((19|20)\d\d))$/;
            if (regex.test(dateString)) {
                var parts = dateString.split("/");
                var dt = new Date(parts[1] + "/" + parts[0] + "/" + parts[2]);
                var dtCurrent = new Date();
                dtCurrent.setMonth(dtCurrent.getMonth() - 6);
                if (dt.getFullYear() <= dtCurrent.getFullYear()) {
                    if (dt > dtCurrent) {
                        sender.innerHTML = "";
                        args.IsValid = true;
                    } else {
                        args.IsValid = false;
                        sender.innerHTML = "Candidate has not worked for 6 months."
                    }
                }
            } else {
                sender.innerHTML = "Enter date in dd/MM/yyyy format ONLY."
                args.IsValid = false;
            }
        }
    </script>
    <asp:TextBox ID="txtDate" runat="server" Text="02/07/2018" />
    <asp:CustomValidator ID="cvDate" runat="server" ClientValidationFunction="ValidateDOB"
        ControlToValidate="txtDate" ErrorMessage="Candidate has not worked for 6 months."
        ValidationGroup="gp1" Display="Dynamic" ForeColor="Red" />
</div>