javascript - Separate 4 digits with : sign using regex -


i working on custom timepicker. want is: if user enters 1234 change 12:34. problem nothing happens @ (i not getting exception). here have far:

// check time entry (function ($) {     string.prototype.timeset = function(){         return this.replace(/^\d{2}\:\d$/,"$1,");     }     $.fn.time = function(){         return this.each(function(){             $(this).val($(this).val().timeset());         })     } })(jquery); 

html markup:

<input id="txttime" type="text" maxlength="4" onpaste="return false;" onchange="this.value = this.value.timeset();" /> 

how can achieve this? regular expression may root of this. note don't want use external mask plugins have apply hot-keys on one.

let's try this:

function formattime(s) {     return s.replace(/\d/g, "").replace(/^(\d\d)(\d\d).*$/, "$1:$2"); }  (function ($) {     $.fn.timeinput = function() {         return $(this).change(function() {             $(this).val(formattime($(this).val()))         });     } })(jquery); 

http://jsfiddle.net/labfq/


Comments