Jump to content
Fórum Script Brasil
  • 0

Validação De Datas


Guest --SirSmart --

Question

Guest --SirSmart --

Galera vocês sabem onde eu posso pegar validção de datas ?

Preciso de um script que funcione no evento onblur.

Se a data estiver errada ele me retorna no campo, até o usuario digitar uma data correta

Se alguém puder me ajudar eu agradeço

Obrigado

Link to comment
Share on other sites

1 answer to this question

Recommended Posts

  • 0

é só adaptar:

<script>
{
function ValidaData()
{
a = document.form.data.value;
var chk    = 0;
var maxDay = 0;

var dd = a.slice(0,2)
var mm = a.slice(3,5)
var yyyy = a.slice(6,10)

maxDay = max_day(mm, yyyy);  

if((dd <= 0) || (dd > maxDay))
{ chk = 1;}
else if((mm <= 0) || (mm > 12))
{ chk = 1;}
else if((yyyy <= 0))
{ chk = 1;} 

if(chk == 1)
{ 
alert('Data Inválida! Tente novamente');
document.form.data.value='';
document.form.data.focus()
}
else
{ 
document.form.submit();
}
}
function max_day(mn, yr)
{
   var mDay;
if((mn == 4) || (mn == 6) || (mn == 9) || (mn == 11))
{ 
mDay = 30;
}
else if(mn == 2)
{
mDay = isLeapYear(yr) ? 29 : 28;    
}
else
{
mDay = 31;
}
return mDay; 

}

function isLeapYear(yr)
{
if (yr % 2 == 0) 
return true;
return false;
}
}
</script>

<form name="form" onsubmit="return ValidaData()">
<input type="text" name="data">
<input type="submit" value="Valida Data">
</form>

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



  • Forum Statistics

    • Total Topics
      152k
    • Total Posts
      651.5k
×
×
  • Create New...