How to set AM/PM in a textbox using JavaScript? Example

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="utf-8" />
    <script src="Scripts/WebForms/jquery-1.7.1.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $("#test1").keypress(function (e) {
                debugger
                var regex = ["[0-2]",
                "[0-4]",
                ":",
                "[0-5]",
                "[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;
            });
        });
    </script>
</head>
<body>
    <form>
        <p>When?</p>
        <input type="text" id="test1" placeholder="hh:mm(AM|PM)" />
    </form>
</body>
</html>

Share:

No comments:

Post a Comment

Tuesday, 22 October 2019

How to set AM/PM in a textbox using JavaScript? Example

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="utf-8" />
    <script src="Scripts/WebForms/jquery-1.7.1.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $("#test1").keypress(function (e) {
                debugger
                var regex = ["[0-2]",
                "[0-4]",
                ":",
                "[0-5]",
                "[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;
            });
        });
    </script>
</head>
<body>
    <form>
        <p>When?</p>
        <input type="text" id="test1" placeholder="hh:mm(AM|PM)" />
    </form>
</body>
</html>

No comments:

Post a Comment

Popular

Total Pageviews

Archive