Solved

Format Time textbox with Javascript

Posted on 2010-09-14
15
310 Views
Last Modified: 2012-05-10
Hi all,

I have a textbox which I made numbers only with Javascript.
My intention is to make this a time textbox but I can't seem to find how to do this.

TimeTextbox:
- first number may max be 2
- second number may max be 3 (or 0 to 9 if the first number is 0 or 1)
- third number may max be 5
- fourth number may max be 9

So tha max value allowed is 2359. I want to add that between entering the second and third number an double point is automaticially entered.
It would format like this:
23:59

I now use this code to allow only digits. How can I check if the number entered does fullfill it's requirements?

I C# you could check on what place the entered value is and then do what ever you want, but how to do this with Javascript.

Can someone point me in the right direction?

Thx
function onlyNumbers(evt)
{
    var e = event || evt; // for trans-browser compatibility
    var charCode = e.which || e.keyCode;

    if (charCode < 48 || charCode > 57 )
        return false;

    return true;
}

Open in new window

0
Comment
Question by:Pit76
  • 9
  • 5
15 Comments
 
LVL 40

Expert Comment

by:gurvinder372
ID: 33673639
<input type="text" onkeydown="onlyTime(event, this)">

function onlyTime(event, thisObj)
{
   var value = parseInt(thisObj.value);
    var e = event || evt; // for trans-browser compatibility
    var charCode = e.which || e.keyCode;

    if (charCode < 48 || charCode > 57 )
        return false;

    switch(value.length)
   {
        case 1:
          if (value > 2)
             return false
          break;    
        case 2:
          if (value > 23)
             return false
          break;    
        case 3:
          if (value%10 > 6)
             return false
          break;    
        case 4:
          if (value%100 > 59)
             return false
   }

    return true;
}
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 33674233
I suggest 2 dropdowns
Impossible to enter wrong data.

0
 

Author Comment

by:Pit76
ID: 33675734
Hi,

The script isn't working. I have added an alert just above the switch and this is the result:
[object Event] -> e
undefined  -> value.length

The value.length is undefined.

This is my HTML part:
<input type="Text" id="workTime" maxlength="5" size="3" value="00:00" onkeydown="onlyTime(event, this)">
0
 
LVL 40

Expert Comment

by:gurvinder372
ID: 33675964
try this

<script>
function onlyTime(event, thisObj)
{
   var value = parseInt(thisObj.value);
    var e = event || evt; // for trans-browser compatibility
    var charCode = e.which || e.keyCode;

    if (charCode < 48 || charCode > 57 )
        return false;
      var valStr = value + "";
    switch(valStr.length)
   {
        case 1:
          if (value > 2)
              thisObj.value = thisObj.value.substring(0, thisObj.value.length-1);
             return false;
          break;    
        case 2:
          if (value > 23)
              thisObj.value = thisObj.value.substring(0, thisObj.value.length-1);
             return false;
          break;    
        case 3:
          if (value%10 > 6)
              thisObj.value = thisObj.value.substring(0, thisObj.value.length-1);
             return false;
          break;    
        case 4:
          if (value%100 > 59)
              thisObj.value = thisObj.value.substring(0, thisObj.value.length-1);
             return false;
   }

    return true;
}
</script>
<body>
<input type="text" onkeyup="onlyTime(event, this)">
</body>

0
 

Author Comment

by:Pit76
ID: 33676917
Hi,

That seems to work, except ffor this part:

   if (charCode < 48 || charCode > 57 )
      {
        return false;
      }

Everything I type is shown. (hjkhklhklh) I added an alert just before the If to show the charcode, and it shows the correct code, it only doesn't execute the If.

Any ideas?

Thx.
0
 

Author Comment

by:Pit76
ID: 33677038
Ok;

I changed it to:
 if (charCode < 48 || charCode > 57 )
      {
            thisObj.value = thisObj.value.substring(0, thisObj.value.length-1);
        return false;
      }

Now it works fine :)
0
 

Author Comment

by:Pit76
ID: 33677055
I forgot,

can you explain what this does?

if (value%10 > 6)

and

if (value%100 > 59)
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:Pit76
ID: 33677295
Sry, not yet what I need. Now I've got this. After the second number it adds a : to it.
When my time is complete 00:00 and I then hit some keys strange things happen.

Can you try with this code?

Thx!


function onlyTime(event, thisObj)
{
   var value = thisObj.value;
    var e = event || evt; // for trans-browser compatibility
    var charCode = e.which || e.keyCode;
	 var valStr = value + "";
	//alert (charCode);
    if (charCode < 48 || charCode > 57)
	{
		thisObj.value = thisObj.value.substring(0, thisObj.value.length-1);
        return false;
	}
	
    switch(valStr.length)
   {
        case 1:
          if (value > 2)
              thisObj.value = thisObj.value.substring(0, thisObj.value.length-1);
             return false;
          break;    
        case 2:
          if (value > 23)
              thisObj.value = thisObj.value.substring(0, thisObj.value.length-1);
			  thisObj.value = thisObj.value + ":";
             return false;
          break;
		// case 3:
		      // thisObj.value = thisObj.value + ":";
          // break;		  
        case 4:
          if (value%10 > 6)
              thisObj.value = thisObj.value.substring(0, thisObj.value.length-1);
             return false;
          break;    
        case 5:
		//alert (valStr.length + "\n\r" + valStr);
          if (value%100 > 59)
              thisObj.value = thisObj.value.substring(0, thisObj.value.length-1);
             return false;
		default:
			return true;
   }
   
    return true;
}

Open in new window

0
 
LVL 40

Expert Comment

by:gurvinder372
ID: 33679394
It is because once you have entered a colon ':' into the value, it cannot be parse into integer.
replace line 3-12 with

var valStr = thisObj.value;
var e = event || evt; // for trans-browser compatibility
var charCode = e.which || e.keyCode;
var value = parseInt(value.replace(":","") );
if (charCode < 48 || charCode > 57)
{
        thisObj.value = thisObj.value.substring(0, thisObj.value.length-1);
        return false;
}
0
 

Author Comment

by:Pit76
ID: 33680844
I get error (undefined is null or not an object)on the following line
var value = parseInt(value.replace(":","") );

Do you get errors when you execute this script?

0
 
LVL 40

Expert Comment

by:gurvinder372
ID: 33681050
ohh sorry :)
it should be
var value = parseInt(valStr.replace(":","") );
0
 

Author Comment

by:Pit76
ID: 33682973
Lol, now everything I enter dissapears from the first number.
If I enter 0 or 1 or 2 the number is deleted again and the box stays empty.
Can't go to the second number.

I appreciate your help on this! I've been a pain in the ass I guess :)
0
 

Author Comment

by:Pit76
ID: 33683138
I found it has something todo with the difference in charcode between numpad numbers and regular numbers.

I added this to the if but it's not entirely correct, it will always be true
if ( (charCode < 96 || charCode > 105) || (charCode < 48 || charCode > 57))

Any idea how I can make this condition correct?

Second, when all numbers are entered, like eg. 12:54 and I press a letter, it deletes the last number, so I guess that I need to check somewhere if the length of the texbox is 5?

Almost there :)
0
 
LVL 40

Accepted Solution

by:
gurvinder372 earned 500 total points
ID: 33689112
This has worked for me, please check
<script>
function onlyTime(event, thisObj)
{
	var valStr = thisObj.value;
	var e = event || evt; // for trans-browser compatibility
	var charCode = e.which || e.keyCode;
	var value = parseInt(valStr.replace(":","") );
	if (charCode < 48 || charCode > 57)
	{
			thisObj.value = thisObj.value.substring(0, thisObj.value.length-1);
			return false;
	}

    switch(valStr.length)
   {
        case 1:
			if (value > 2)
			{
				thisObj.value = thisObj.value.substring(0, thisObj.value.length-1);
				return false;
			}
			break;    
        case 2:
			if (value > 23)
			{
				thisObj.value = thisObj.value.substring(0, thisObj.value.length-1);
				return false;
			}
			thisObj.value = thisObj.value + ":";
          break;    
        case 4:
			if (value%10 > 6)
			{
				thisObj.value = thisObj.value.substring(0, thisObj.value.length-1);
				return false;
			}
          break;    
        case 5:
			if (value%100 > 59)
			{
				thisObj.value = thisObj.value.substring(0, thisObj.value.length-1);
				return false;
			}
	}

    return true;
}
</script>
<body>
<input type="text" onkeyup="onlyTime(event, this)" maxlength="5">
</body>

Open in new window

0
 

Author Closing Comment

by:Pit76
ID: 33734693
there is still a problem when using backspace after the last digit is entered.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
This article discusses four methods for overlaying images in a container on a web page
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now