Write hour with the character ":" (javascript ot jquery)

Hi E's,
I have a form with a input for customer write the hour and minutes, in this format: hh:mm.
I need a solution for the character ":" always be present automatically when customer write the hour and the minutes, like this (example for 20:30):
1 character: 2:
2 character: 20:
3 character: 20:3
4 character: 20:30
I try to found a solution in Internet, but I don't know the correct phrase for this subject.
Can you help me please.

The best regards, JC
LVL 4
Pedro ChagasWebmasterAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Lee SavidgeCommented:
if(myTimeField.length == myTimeField.replace(":", "").length)
{
 alert("Time format invalid");
}

Open in new window

Lee SavidgeCommented:
Oh hang on, I misread your question :) My bad. Let me rethink.
Lee SavidgeCommented:
This is something you can only really do when you leave the field, so the onblur event would be the best bet.

Something like:

<input id=myTimeField onblur=fixTimeFormat()>

<script>
function fixTimeFormat()
{
  var iLen = myTimeField.length;

  if(iLen == myTimeField.replace(":", "").length)
  {
    // The : is missing so we need to insert it
    if(iLen == 1 || iLen == 2)
      document.getElementById("myTimeField").value = document.getElementById("myTimeField").value + ":";
    else if(iLen == 3)
      document.getElementById("myTimeField").value = document.getElementById("myTimeField").substr(0,2) + ":" + document.getElementById("myTimeField").substr(2,1);
    else if(iLen == 4)
      document.getElementById("myTimeField").value = document.getElementById("myTimeField").substr(0,2) + ":" + document.getElementById("myTimeField").substr(2,2);
  }
}

</script>

Open in new window


I've not tested this myself but I'd do it this way. Easy to read. There may well be more elegant solutions or more efficient solutions, but I prefer readability in code more than anything unless performance is a particular issue.
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Julian HansenCommented:
You can use an input mask library like this one
https://github.com/RobinHerbots/jquery.inputmask

HTML
<input type="text" name="hour" data-inputmask="'mask': '99:99'"/>

Open in new window

Javascript
<script src="http://code.jquery.com/jquery.js"></script>
<script src="https://raw.githubusercontent.com/RobinHerbots/jquery.inputmask/3.x/js/inputmask.js"></script>
<script src="https://raw.githubusercontent.com/RobinHerbots/jquery.inputmask/3.x/js/jquery.inputmask.js"></script>
<script>
$(function(){
  $(":input").inputmask();
});
</script>

Open in new window

Working sample here

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Lee SavidgeCommented:
See, I knew there would be a better option ^^
Pedro ChagasWebmasterAuthor Commented:
Hi, @Lee Savidge,
Your solution don't work.
Do you want to fix?

The best regards, JC
Lee SavidgeCommented:
Do you get an errors? As I said I hadn't tested it and wrote it off the top of my head. Give me a minute.
Lee SavidgeCommented:
I had a few simple errors. This works:

<input type=txt id=myTimeField onblur=fixTimeFormat() maxlength=5>
<input type=submit value=Submit>
<script>
function fixTimeFormat()
{
  var myTimeField = document.getElementById("myTimeField");
  var iLen = myTimeField.value.length;
    

  if(iLen == myTimeField.value.replace(":", "").length)
  {
    // The : is missing so we need to insert it
    if(iLen == 1 || iLen == 2)
      document.getElementById("myTimeField").value = document.getElementById("myTimeField").value + ":";
    else if(iLen == 3)
      document.getElementById("myTimeField").value = document.getElementById("myTimeField").value.substr(0,2) + ":" + document.getElementById("myTimeField").value.substr(2,1);
    else if(iLen == 4)
      document.getElementById("myTimeField").value = document.getElementById("myTimeField").value.substr(0,2) + ":" + document.getElementById("myTimeField").value.substr(2,2);
    else if(iLen > 4)
    {
      alert("Too many characters in string");
    }
  }
}

</script>

Open in new window


See here: https://jsfiddle.net/9ku52er7/
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.