Input text of hours!

Asked

Viewed 634 times

2

Good afternoon, I need a team-validated mask where Hours accepts until 23 and minutes until 59 someone could help me ?

2 answers

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");
        };
    }
});
  • Yes friend I use, but Mask accepts as hours 99:99 In my case my hours field can accept at most 23:59

  • Edited in the reply

  • I’ll test just a moment!

  • didn’t work buddy

  • Complete your question with your HTML structure and the JS piece where you run the function

1


Try it buddy:

<script src="https://code.jquery.com/jquery-git.min.js"></script>
    <script src="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 in my system, thank you very much!

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();
            }

        }
    }
});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.