rodrigosantosbr

[JS] Date Validation

Jan 17th, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!

http://jsbin.com/?html,js,output


html

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
  Dia: <input type="text" id="dia"/><br/>
  mes: <input type="text" id="mes"/><br/>
  ano: <input type="text" id="ano"/><br/>
  <input type=button onclick="submeter()" value="Data">
</body>
</html>

javascript

function submeter(){
  dia = parseInt(document.getElementById("dia").value);
  if (dia <= 9){
    dia = "0" + dia
  }
  mes = parseInt(document.getElementById("mes").value);
  if (mes <= 9){
    mes = "0" + mes
  }
  ano = document.getElementById("ano").value;
  validar = validarData(ano+"-"+mes+"-"+dia);
  alert(validar);
}
function validarData( data ) {
  var re = /^\d{4}[\-\/\s]?((((0[13578])|(1[02]))[\-\/\s]?(([0-2][0-9])|(3[01])))|(((0[469])|(11))[\-\/\s]?(([0-2][0-9])|(30)))|(02[\-\/\s]?[0-2][0-9]))$/;
  if(re.exec(data)){
    return true;
  }
  return false;
}
Add Comment
Please, Sign In to add comment