Input text of hours! [closed]

2

Good afternoon, I need a valid team mask where Hours accept up 23 and minutes up 59 could anyone help me?

    
asked by anonymous 04.05.2017 / 20:50

2 answers

1

Try this friend:

<script src="https://code.jquery.com/jquery-git.min.js"></script><scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/jquery.mask/1.14.10/jquery.mask.min.js"></script>
    <input type="text" id="txtTesteMask" />
    <script type="text/javascript">
        var txtMask = $("#txtTesteMask");
        txtMask.mask("99:99");
        txtMask.blur(function () {
            if (txtMask.val().search(":") == -1 || txtMask.val().length != 5) {
                alert('erro');
                txtMask.focus();
            }
            else {

                var horaseMinutos = txtMask.val().split(':');
                if (!$.isNumeric(horaseMinutos[0]) || !$.isNumeric(horaseMinutos[1])) {
                    alert('erro');
                    txtMask.focus();
                } else {

                    var horas = parseInt(horaseMinutos[0]);
                    var minutos = parseInt(horaseMinutos[1]);
                    if (horas < 0 || horas > 23) {
                        alert('erro');
                        txtMask.focus();
                    } else
                        if (minutos < 0 || minutos > 59) {
                            alert('erro');
                            txtMask.focus();
                        }
                }
            }
        })
    </script>

Friend worked, I already approved and made a modification on my system, thank you!

var txtMask = $("#txtTesteMask");
txtMask.mask("99:99");
txtMask.blur(function () {
    if (txtMask.val().search(":") == -1 || txtMask.val().length != 5) {
        if (txtMask.val().search(":") == -1 || txtMask.val().length != 5) {
            $('[name="hea1"]').val('');
            $('#hea1-msg').show();
            $('#hea1-msg').text('Horario invalido.');
        } else {
            $('#hea1-msg').hide();
        }
    }
    else {

        var horaseMinutos = txtMask.val().split(':');
        if (!$.isNumeric(horaseMinutos[0]) || !$.isNumeric(horaseMinutos[1])) {
            if (!$.isNumeric(horaseMinutos[0]) || !$.isNumeric(horaseMinutos[1])) {
                $('[name="hea1"]').val('');
                $('#hea1-msg').show();
                $('#hea1-msg').text('Horario invalido.');
            }
            else {
                $('#hea1-msg').hide();
            }
        } else {

            var horas = parseInt(horaseMinutos[0]);
            var minutos = parseInt(horaseMinutos[1]);
            if (horas < 0 || horas > 23) {
                $('[name="hea1"]').val('');
                $('#hea1-msg').show();
                $('#hea1-msg').text('Horario invalido.');
            } else {
                $('#hea1-msg').hide();
            }

        }
    }
});
    
04.05.2017 / 21:06
3

You can use the jQuery Masked Input

For example:

$.mask.definitions['H'] = "[0-2]"; // seta as mascaras
$.mask.definitions['h'] = "[0-9]";
$.mask.definitions['M'] = "[0-5]";
$.mask.definitions['m'] = "[0-9]";
$("#horario").mask("Hh:Mm", {
    completed: function() { // valida o horario
        var currentMask = $(this).mask();
        if (isNaN(parseInt(currentMask))) {
            $(this).val("");
        } else if (parseInt(currentMask) > 2359) {
            $(this).val("23:59");
        };
    }
});
    
04.05.2017 / 20:54