Máscara para data ou hora

terça-feira, 25 de maio de 2010
<html>
    <head>
        <title>formatado data em javascript</title>
        <script type="text/javascript">
            function Formatadata(Campo, teclapres)
           {
               var tecla = teclapres.keyCode;
               var vr = new String(Campo.value);
               vr = vr.replace("/", "");
               vr = vr.replace("/", "");
               vr = vr.replace("/", "");
               tam = vr.length + 1;
               if (tecla != 8 && tecla != 8)
              {
                   if (tam > 0 && tam < 2)
                      Campo.value = vr.substr(0, 2) ;
                   if (tam > 2 && tam < 4)
                      Campo.value = vr.substr(0, 2) + '/' + vr.substr(2, 2);
                   if (tam > 4 && tam < 7)
                      Campo.value = vr.substr(0, 2) + '/' + vr.substr(2, 2) + '/' + vr.substr(4, 7);
              }
           }
        </script>
    </head>
    <body>
        <form method="post" action="arquivo.php">
        <p>
            <label>Data:</data>
            <input type="text" name="data" maxlength="10" onkeyup="Formatadata(this,event)" />
        </p>
        </form>
    </body>
</html>

0 comentários:

Postar um comentário