Click here to Skip to main content
15,916,835 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Hi,
I would like to put a default date format on an asp.net textbox to dd/MM/yyyy without any masking tools.

Can someone please help me, I did try google but never found what I quite want.
Posted
Comments
Suvendu Shekhar Giri 24-Jul-15 8:59am    
If you don't want to use any third party files then try writing yourself.
Javascript/JQuery should be your weapons for this.
You can use JQuery .keyup() function to put / automatically after 2/4 characters and also write logic to accept numbers and valid values for different section.

1 solution

As suggested in the comments, jQuery will be the choice for you. Add a new JavaScript file to your project and add the following code to it. Make sure to reference it in the page for it to work. You will need a reference to jquery-1.10.2.min.js as well.

Using the Id of the control as inDate for explanation.

C#
$(document).ready(function() {
    $('#inDate').focus(function () {
        var inDate = $(this).val();
        if (inDate == 'dd/MM/yyyy') {
            $(this).val('');
        }
    });
    $('#inDate').blur(function () {
        var inDate = $(this).val().trim();
        if (inDate == '') {
            $(this).val('dd/MM/yyyy');
        }
    });
});


focus event will be triggered whenever the control with id = inDate gets focus. The blur event will be triggered when it looses focus. Refer the following links for more details.

https://api.jquery.com/focus/[^]
https://api.jquery.com/blur/[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900