Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Format date while typing 1

Status
Not open for further replies.

Schultjeff

Technical User
Sep 27, 2002
10
0
0
US
I am moving a windows forms app to an ASP.net app. In the windows forms app, if the user is typing into a textbox that accepts dates, the textbox automatically includes the ":" (colon) when the user gets to the third space. Can this be done in an ASP.net program? I am assuming that I will have to use Javascript to accomplish the feat.

Jeff
 
JavaScript is a handy way to format all kinds of things on-the-fly.

Exactly what sort of validation do you need?

Cheers,
[monkey] Edward [monkey]

"Cut a hole in the door. Hang a flap. Criminy, why didn't I think of this earlier?!" -- inventor of the cat door
 
Javascript is my problem. I know little to none. It is funny how the ASP.net marketing says that you do not need it, but thier controls are very limited without.

I need a colon inserted into the third space in a text box after the user types the first two digits of a time. Then the user should be able to continue typing into the forth space. Using windows forms (vb) I would use a combination on Sellength and Selposition to accomplish this.
 
Are you validating time or a date? I'm presuming a time. But it doesn't matter.

So, first, save this as "Sample.js":

Code:
function ValidateTime(Argument)
  {
    if((Argument.length == 2)||(Argument.length == 5))
      { Argument=Argument+":" }
    if(Argument.length == 8)
      { Argument=Argument+"."; }
    return Argument;
  }

Then save this as "Sample.html":

Code:
<!DOCTYPE html 
     PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot;
     &quot;[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;>[/URL]

<html xmlns=&quot;[URL unfurl="true"]http://www.w3.org/1999/xhtml&quot;[/URL] xml:lang=&quot;en&quot; lang=&quot;en&quot;>
  <head>
    <meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=iso-8859-1&quot;></meta>
    <script src=&quot;Sample.js&quot; type=&quot;text/javascript&quot;></script>
    <title>JavaScript Sample</title>
  </head>
  <body>
    <form name=&quot;Scootchy&quot;>
      <p>Input a time: <input type=&quot;text&quot; name=&quot;VarName&quot; id=&quot;VarName&quot; value=&quot;&quot; onkeyup=&quot;document.Scootchy.VarName.value=ValidateTime(document.Scootchy.VarName.value);return true;&quot;></input></p>
    </form>
  </body>
</html>

And run &quot;Sample.html&quot; in your browser. This just does a dumb-cut at the hours, minutes, seconds, and decimal seconds. Go ahead, start typing numbers into the text box.

Izzat what you're looking for?

Cheers,
[monkey] Edward [monkey]

&quot;Cut a hole in the door. Hang a flap. Criminy, why didn't I think of this earlier?!&quot; -- inventor of the cat door
 
It is more that what I needed. It is perfect!

I feel that I owe you my first born child. Where should I mail him?

Thanks

Jeff
 
Glad it worked for you! No need for kids, though. Well, actually, there are a few chores around the house. How old is he? [lol]

Cheers,
[monkey] Edward [monkey]

&quot;Cut a hole in the door. Hang a flap. Criminy, why didn't I think of this earlier?!&quot; -- inventor of the cat door
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top