How to validate with Javascript an Input text with Hours and Minutes

后端 未结 8 1093
灰色年华
灰色年华 2020-12-28 13:49

I have to build an HTML Form with an Input text field for Hours and Minutes.
Something like:

Name : [Foo]
Surname

8条回答
  •  星月不相逢
    2020-12-28 14:00

    How about

    function validTime(inputStr) {
        if (!inputStr || inputStr.length<1) {return false;}
        var time = inputStr.split(':');
        return time.length === 2 
               && parseInt(time[0],10)>=0 
               && parseInt(time[0],10)<=23 
               && parseInt(time[1],10)>=0 
               && parseInt(time[1],10)<=59;
    }
    

提交回复
热议问题