Click here to Skip to main content
15,843,070 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my web page I have date field. When the user type date such format like

13-4-2012,13.4.2012 This are all automatically changed this format

04/13/2012

And Which is not allowed to type alphabets,And It allow only dates. How to do it using Javascript code. Please help me anybody

Thanking you
Posted

You can use this java script code,where datepicker is the id of your control which hold the date.

JavaScript
<script type="text/javascript">
    $(function() {
        $('#datepicker').datepicker({ dateFormat: 'dd/mm/yy' });
        $("#datepicker").datepicker();
    });
</script></script>
 
Share this answer
 
Comments
Prasad_Kulkarni 29-May-12 7:22am    
Formally 'Accept solution' if it works for you.
Please refer: Date time formats[^]

Or else,

Try this:
SQL
SELECT CONVERT(VARCHAR(10), GETDATE(), 101) AS [MM/DD/YYYY]


[EDIT]

JavaScript:

Javascript Convert String to Date[^]

you can use date format function written by Steven Levithan here[^]
C#
var date = dateFormat(new Date("Thu Oct 14 2010 00:00:00 GMT 0530 (India Standard Time)"), 'dd/mm/yyyy');

Have look on: DateJs[^]
 
Share this answer
 
v2
I think your requirements can be easily fulfilled if you use this calender

http://www.javascriptkit.com/script/script2/epoch/index.shtml[^]

with this script on keypress

XML
<asp:textbox id="TextBox6" runat="server" onkeypress='return AcceptDateCharacters(event, "/");'></asp:textbox>


and this javascript function to restrict the input

JavaScript
function AcceptDateCharacters(event, separator)
{
    if(separator.length != 1)  //only pass single character separators here
    {
        return false;
    }
    //lets allow digits
    var expression = "^[0-9";
    
    //lets allow the separator character
    expression += separator;
    
    //lets complete the expression
    expression += "]$";
    
    var regex = new RegExp(expression);    
    return AcceptRegExOnly(event, regex)  
};


I wrote this couple of years back and till date it is working fine.
 
Share this answer
 
v2
There are multiple ways to handle it. To me, having a custom control - a masked textbox being the best one. Alternatively, you need to handle the onKeyPress of textbox in JavaScript to avoid alphabets. Use, OnFocusLost to format into desired way.

Look these:
AjaxToolkit: MaskedEdit Demonstration[^]
Enhanced Textbox Control[^]
Input mask - a script that automatically enters a specified character at a certain point in a text box.[^]
Xtended TextBox - Customized data entry[^]
Mask TextBox ASP.NET Control[^]
 
Share this answer
 
Comments
devausha 29-May-12 8:07am    
I am using MaskedEditExtender. When I convert the text value which is linked MaskedEditExtender to datetime it displays Error.
DateTime t1 = Convert.ToDateTime(txtdate.Text);
The error is String was not Convert to datetime

Please help me
Sandeep Mewara 29-May-12 8:21am    
Check what is the value of 'txtdate.Text'. Looks like not a valid date string.
devausha 29-May-12 8:32am    
Oh Thank you. Error is Calender Extender. Now it is working
Sandeep Mewara 29-May-12 8:47am    
Good to know.

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