Click here to Skip to main content
15,616,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a javascript file, which creates a calendar picker. This file is used through out the application and is a piece of code that I have to use because my .Net application is part of another application which uses this piece of JavaScript code.

Anyhow, with the calendar picker I can save the date into a textbox, but the problem that I have is the format. Unfortunately, I cannot change the calendar picker code because this will affect other parts of the application. Therefore, what I am looking to do is to reformat the date on the textbox control. This I can do but only when I use the javascript event onfocus.

Now, my question is this, is there away in javascript or with jquery that when a value enters into a textbox that this value is then reformatted to the format I want without postback or clicking on the textbox?


Do what you want with the input, see the example below which reads a text box and spits it back out to a paragraphs innerhtml;

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<html xmlns="" >


    <title>Untitled Page</title>




        Some HTML Page</p>


        <input id="Text1" type="text" onchange ="return doFormat()" /></p>

    <p id="para1">


        <script type="text/javascript">

            function doFormat() {

                var something = Text1.value;

                //Do what ever parsing and formatting then
                //spit it back out to paragraph.

                para1.innerHTML = "you entered: " + something.toString();




Share this answer
AspDotNetDev 31-Aug-10 18:01pm    
This would not work. For one, the "onchange" event doesn't work for programmatically set text, as doing so would create a recursive call when setting the text again programmatically. Second, the onchange event is only fired when some other event happens, such as when the user types stuff and then clicks out of the textbox. This may depend on the browser though.
DaveAuld 31-Aug-10 18:13pm    
Agree with the recursive calls, the demo among was just showing how to pickup the existing value and dump it some where else (the user can come up with his own formatting scheme.
If it is for display purposes only, you could have another textbox with the new formatted value and hide the old original. You could show and hide these using JQuery.
AspDotNetDev 31-Aug-10 18:19pm    
I'm pretty sure the onchange event will not fire when the calendar control changes its value. That means even if you use a different textbox and hide the original, there will still be no event to notify your code of the change.
Does the calendar control you are using have an OnChange event (or something like that) that you can listen to? A click event or something like that may work as well. If changing the date/time causes a postback, that is another opportunity for you to format the textbox. If so, you can put your code to do the format there. Otherwise, you can create a timer that checks for changes every second or so. You might even hide the textbox and show another one that you have control over in its place. That would be useful in the case of the time, because then you wouldn't see the incorrect format between when it gets set in the textbox and when you update it.
Share this answer
Oh, and one more idea. Can the calendar control be set to postback? If so, then you can use a little AJAX magic to update the textbox without refreshing the whole page. You can put the calendar control inside an UpdatePanel. When the AJAX postback occurs, you can register a client script that will run when the postback returns to the page. See more about that technique here.
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