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

后端 未结 8 1035
灰色年华
灰色年华 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:03

    might not be the best but this works for me. i am returning false if there is no error in this case. checks for both format and values.

    if (value.substr(2,1) === ':') {
        if (!value.match(/^\d\d:\d\d/)) {
            return "Invalid Format";
        }
        else if (parseInt(value.substr(0,2)) >= 24 || parseInt(value.substr(3,2)) >= 60)  {
            return "Invalid Format";
        }
        else {
            return false;
        }
    
    }
    else {
        return "Invalid Format";
    }
    

提交回复
热议问题