HTML / JS text field for date does not work in Firefox

0

I have the code below as an input for the date, it works fine on all browsers except Firefox. It probably has something to do with event.returnValue=false being ignored by the browser, how do I fix this?

        <html>
    <script language="JavaScript">
        function Data(evento, objeto){
            var keypress=(window.event)?event.keyCode:evento.which;
            campo = eval (objeto);
            if (campo.value == '00/00/0000')
            {
                campo.value=""
            }

            caracteres = '0123456789';
            separacao1 = '/';
            conjunto1 = 2;
            conjunto2 = 5;          

            if ((caracteres.search(String.fromCharCode(keypress))!=-1) && campo.value.length < (10))
            {
                if (campo.value.length == conjunto1 )
                        campo.value = campo.value + separacao1;
                else if (campo.value.length == conjunto2)
                        campo.value = campo.value + separacao1;
            }
                else
                        event.returnValue = false;
         }  

    </script>
</head>
<body>
    <form method=post action="">
        Data: <input type="text" name="txtdata" pattern="[0-9]" maxlength="10" size="10" onKeyPress="Data(event, this)">
    </form>
</body>
    
asked by anonymous 16.08.2017 / 21:20

1 answer

0

This feature is not really compatible with Firefox.

For this action, you can use event.preventDefault() or simply return false for function ( return false ).

Your code changed:

<html>
    <script language="JavaScript">
        function Data(evento, objeto){
            var keypress=(window.event)?event.keyCode:evento.which;
            campo = eval (objeto);
            if (campo.value == '00/00/0000')
            {
                campo.value=""
            }

            caracteres = '0123456789';
            separacao1 = '/';
            conjunto1 = 2;
            conjunto2 = 5;          

            if ((caracteres.search(String.fromCharCode(keypress))!=-1) && campo.value.length < (10))
            {
                if (campo.value.length == conjunto1 )
                        campo.value = campo.value + separacao1;
                else if (campo.value.length == conjunto2)
                        campo.value = campo.value + separacao1;
            }
                else
                        evento.preventDefault();
         }  

    </script>
</head>
<body>
    <form method=post action="">
        Data: <input type="text" name="txtdata" pattern="[0-9]" maxlength="10" size="10" onKeyPress="Data(event, this)">
    </form>
</body>
    
17.08.2017 / 21:16