0

I want to apply a date mask (dd/mm/yyyy) on a ASP.net textbox

I tried many javascript solutions from google search, unfortunately nothing is working properly, especially when backspace.

Can anyone please guide me a script?

Cristik
  • 30,989
  • 25
  • 91
  • 127
Srinivas
  • 173
  • 1
  • 2
  • 11
  • Not sure what you're trying to do... Can you post the part of the code you're working on, and what you're trying to achieve ? – Kewin Dousse Aug 24 '16 at 08:08
  • Refer this : [How to do date masking using javascript (without JQuery)?](http://stackoverflow.com/questions/31108620/how-to-do-date-masking-using-javascript-without-jquery) – Divyang Desai Aug 24 '16 at 08:09
  • Hi @Protectator, thanks for the quick reply. I already tried the suggestion in the link you provided. Its working improperly. backspace is not working properly, there is no control on the alphabets. – Srinivas Aug 24 '16 at 08:57

1 Answers1

0
    <asp:TextBox ID="txtDate2" runat="server" Text="11/11/2011" />
    <asp:CustomValidator runat="server" ClientValidationFunction="ValidateDate" ControlToValidate="txtDate2"
    ErrorMessage="Invalid Date." ValidationGroup="Group2" />
<br />
<br />
<asp:Button ID="Button2" Text="Validate" runat="server" ValidationGroup="Group2" />
<script type="text/javascript">
    function ValidateDate(sender, args) {
        var dateString = document.getElementById(sender.controltovalidate).value;
        var regex = /(((0|1)[1-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]);
            args.IsValid = (dt.getDate() == parts[0] && dt.getMonth() + 1 == parts[1] && dt.getFullYear() == parts[2]);
        } else {
            args.IsValid = false;
        }
    }
</script>
Ritesh
  • 9
  • 5