jquery - Not getting a way to apply regEx to time input -


i working on timepicker , want restrict user enter time more 23:59. trying using regex not getting succeed

fiddle demo

html:

<input type="text" id="test1" placeholder="hh:mm"/> 

jquery

$("#test1").keypress(function(e) {     var regex = ["([01]?[0-9]|2[0-3])",":","[0-5][0-9]"],     string = $(this).val() + string.fromcharcode(e.which),b = true;     (var = 0; < string.length; i++) {         if (!new regexp("^" + regex[i] + "$").test(string[i])) {             b = false;         }         }     return b; }); 

it's not giving error can't figure out happening.

any appreciated.

thank you

you test every character against own regular expression;

fiddle

try

$("#test1").keypress(function(event)  {    var regexs = [/[0-2]/,/[0-3]/,/:/,/[0-5]/,/[0-9]/];     var key = event.which;     var string = $(this).val() + string.fromcharcode(key)     var characters  = string.split("");     var passed = true;     var isbackspace = key === 8;     var shouldtest = characters.length < 5 && ! isbackspace;     passed = ! ( characters.length > 5 && ! isbackspace );     if(shouldtest)    {        (var = 0; < characters.length; i++)         {            var character = characters[i];            var regex = regexs[i];             var testfailed = ! regex.test(character) ;             if( testfailed )             {                 passed = false;                 break;            }         }    }     return passed;  }); 

Comments