input data freestanding javascript

-1

I created a registration page so that the java function would be coupled to the "/" symbol. example typing 20 the java docked "/" = > 20 / entering 2 more digits = > 20/12 / filtering to the default date 20/13/2017. but after typing the month nothing happened. What's wrong?

<fontcolor="orange" size="4">Data:<font><p></p>
<input type="text" required maxlength="10" id="data" onkeyup="datajs()" placeholder="dia/mes/ano" />

<script>
    function datajs(){

    data = document.getElementById("data").value;
    var dia = data.substr(0,2);
    var mes = data.substr(2,3);
    var ano = data.substr(5,4);
    var data1 = data.substr(2,1);
    var data2 = data.substr(5,1);
    if (data1 != "/") {
        if( data.length >= 3 ) {
              if( data.length <= 4 ) {
                  document.forms[0].data.value = dia+'/'+mes;
       }
   }
}



    if(data2 != "/") {
        if( data.length > 5 ) {
            if( data.length <= 10 ) {
            document.forms[0].data.value = dia+mes+'/'+ano;
           }
        }
      }
    }
 </script>

 <style>
 #data {

    
asked by anonymous 03.03.2017 / 18:04

1 answer

0

Denilson, I have used this but with jQuery and Masked, very practical and will serve you for various other text formatting, dates and etc ...

Download jQuery and Masked on jQuery's own website.

<html>
  <head>
    <script type="text/javascript" src="js/jquery-3.1.1.min.js"></script>
    <script type="text/javascript" src="js/jquery.maskedinput.js"></script>
  </head>
 <body>
  <font color="orange" size="4">Data:<font><p></p>
  <input type="text" required maxlength="10" id="data" placeholder="dia/mes/ano" />

  <script>
    $(document).ready(function () {
      $("#data").mask("99/99/9999");
    });
 </script>
 </body>
</html>
    
03.03.2017 / 18:38