Regex for AM PM time format for jquery

后端 未结 6 439
耶瑟儿~
耶瑟儿~ 2020-12-09 17:16

I am being frustrated by a regular expression to mask the input field. I want to Limit input to hh:mm AM|PM format and I can\'t get this regex to work.

I use this

6条回答
  •  攒了一身酷
    2020-12-09 17:50

    You can't do that with that plugin because here you need to check each character.

    HTML:

    When?

    JavaScript:

    $("#test1").keypress(function(e) {
        var regex = ["[0-2]",
        "[0-4]",
        ":",
        "[0-6]",
        "[0-9]",
        "(A|P)",
        "M"],
        string = $(this).val() + String.fromCharCode(e.which),
        b = true;
        for (var i = 0; i < string.length; i++) {
            if (!new RegExp("^" + regex[i] + "$").test(string[i])) {
                b = false;
            }
        }
        return b;
    });
    

    Example

提交回复
热议问题