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

Asked

Viewed 85 times

0

I have the code below as an input for the date , works well on all browsers except in Firefox. It probably has something to do with the event.returnValue=false be ignored by browser, as 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>
  • 1

    What is the intention with this Event.returnValue = false?

  • The intention is that the input box does nothing if the if is not true, but firefox simply inserts the typed key as text input.

1 answer

0


This function is not really compatible with Firefox.

For this action, you can use the event.preventDefault() or simply return false to Function (return false).

Your changed code:

<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>
  • I made the change to preventDefault() and later to Return false, however I got the same result, the code works normally in Chrome, opera and safari, but not in firefox.

  • I managed to make it work, as I called Event Event in the function call I just needed to change to evento.preventDefault(); Thank you very much Diego.

  • @Juscelinodorneles It’s true, I put Event and it should be event, cool that managed to fix, took advantage and changed the answer. abs

Browser other questions tagged

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