Regex for AM PM time format for jquery

后端 未结 6 440
耶瑟儿~
耶瑟儿~ 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:

    <form>
        <p>When?</p>
            <input type="text" id="test1" placeholder="hh:mm(AM|PM)"/>
    </form>
    

    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

    0 讨论(0)
  • 2020-12-09 17:53

    So I'm pretty sure this is solved by now, but I was recently struggling with this and couldn't find an answer fast enough. I'm using Bootstrap Validator (bootstrapvalidator.com by @nghuuphuoc) in conjunction with Eonasdan's DateTimepicker to validate an hour (PS: You can disable the date in Eonasdan's plugin).

    Since Bootstrap Validator doesn't yet have a validator for time, you have to use a Regex. This one worked perfectly for me:

    ^([0-1]?[0-9]|2[0-3]):[0-5][0-9] [APap][mM]$

    0 讨论(0)
  • 2020-12-09 17:54

    First of all, if you using it for input field, you should never let users input date or time information using text fields and hoping it will be in strict format.

    But, if you insist:

    /^(0?[1-9]|1[012])(:[0-5]\d) [APap][mM]$/
    

    This regex will validate time in AM/PM format.

    0 讨论(0)
  • 2020-12-09 17:55

    the follwing function return (true/false) value

        function CheckTime(HtmlInputElement) {
           var dt = HtmlInputElement.value;
           return (/(0?[1-9]|1[0-2]):[0-5][0-9] ?[APap][mM]$/.test(dt));
    }
    
    0 讨论(0)
  • 2020-12-09 17:59

    Check this solution too.

    <!DOCTYPE html>
    <html>
    <body>
    
    <p>This demo used to validate time with 12 hours format</p>
    
    <button onclick="myFunction()">Try it</button>
    
    <p id="demo"></p>
    
    <script>
    function myFunction() {
        var regexp = /^(0?[1-9]|1[012])(:[0-5]\d) [APap][mM]$/;
        var res = regexp.test('12:00 AM'); //try with alphabets or wrong format
        document.getElementById("demo").innerHTML = res;
    }
    </script>
    
    </body>
    </html>
    
    0 讨论(0)
  • 2020-12-09 18:01

    I have made a jsfiddle example, based on the regular expression of the answer von Yuri: http://jsfiddle.net/Evaqk/

    $('#test1, #test2').blur(function(){
        var validTime = $(this).val().match(/^(0?[1-9]|1[012])(:[0-5]\d) [APap][mM]$/);
        if (!validTime) {
            $(this).val('').focus().css('background', '#fdd');
        } else {
            $(this).css('background', 'transparent');
        }
    });
    
    0 讨论(0)
提交回复
热议问题