javascript standalone data input

Asked

Viewed 120 times

-1

I created a registration page for the java function to match the "/" symbol. example by typing 20 java engaged "/" => 20/ typing 2 more digits => 20/12/ filtering to the standard date 20/13/2017 . but after typing the month nothing happened. what’s wrong ?inserir a descrição da imagem aqui

<font color="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 {

inserir a descrição da imagem aqui

  • Difficult to understand the question. Elaborate better.

1 answer

0

Denilson, I already used this but with jQuery and Masked, very practical and will serve you for several other text formats, 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>

Browser other questions tagged

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