Click here to Skip to main content
11,572,251 members (44,642 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Javascript
Hi

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?

Thanks
Posted 31-Aug-10 10:55am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml" >
 
<head>
 
    <title>Untitled Page</title>
 
</head>
 
<body>
 
    <p>
 
        Some HTML Page</p>
 
    <p>
 
        <input id="Text1" type="text" onchange ="return doFormat()" /></p>
 
    <p id="para1">
 
        &nbsp;</p>
 
        <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();
 
            }
 
        </script>
 
</body>
 
</html>
  Permalink  
Comments
aspdotnetdev at 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 at 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 at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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.
  Permalink  

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

  Print Answers RSS


Advertise | Privacy | Mobile
Web03 | 2.8.150624.2 | Last Updated 31 Aug 2010
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100