javascript - How do I auto-insert a leading zero in Time notation -


the following code checks if user inserted correct time notation in textbox. if notation not correct alert box shown.

it shows alert if leading 0 forgotten (i.e. 7:45 iso 07:45)

function validatethis(e){     //input validation         var regexp = /([01][0-9]|[02][0-3]):[0-5][0-9]/; //correct time format         var correct = (e.value.search(regexp) >= 0) ? true : alert('enter time hh:mm (13:25)');     } 

my question how can auto-insert leading 0 if forgotten rather notifying user

you may try following approach:

function verify()  {    var str=$("#time").val();  var patt = new regexp("^(0?[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$");    if(patt.test(str))    {      if(str.length==4)        $("#time").val("0"+str);      console.log("true");    }  else    console.log("false");        }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>  <input type="text" name="time" id="time" value="" />  <input type="button" name="sbmt" id="sbmt" value="submit" onclick="verify();">


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -