Jump to content

date validation


verN

Recommended Posts

hi, i have a text box where the user enters the date however, iwould like to validate the user's input by checking that the user have

 

(1) inputted the date in the following format DD/MM/YY

(2) and words are not entered e.g dkfgsdjg

(3) the day, month, year are correct e.g don't allow 32/14/2006

 

 

 

$date = $_POST['dates']; 

 

 

how could i do this, i'm thinking of using javascript but do not know how to go about solving this probelm. I would like a message outputted telling the user the date they entered e.g. 25/44/2007 was incorrect and they should input a date in the cprrrect format e.g. DD/MM/YYYY.

 

thanks

Link to comment
https://forums.phpfreaks.com/topic/45883-date-validation/
Share on other sites

<head>
<script language='javascript'>
function validiate(myform)
{
if(!myform) { myform="frm"; }
day = document.forms[myform].dd.value;
month = document.forms[myform].mm.value;
year = document.forms[myform].yy.value;
checklength('day',day,2);
checklength('month',month,2);
checklength('year',year,2);
checktype('day',day);
checktype('month',month);
checktype('year',year);
}
function checklength(field,val,len)
{
thesize = val.length;
if(thesize > len){ alert('The '+field+' field is too long it must be no longer than '+len+' characters.');}
} 
function checktype(field,val)
{
if (isNaN(val)){ alert('The field '+field+' is invalid, Only numbers allowed.');}
}
</script>
</head>
<body>
<form name='frm'>
<input size='2' name='dd' maxlength='2' value='dd'>
<input size='2' name='mm' maxlength='2' value='mm'>
<input size='2' name='yy' maxlength='2' value='yy'>
<input type='button' onclick='validiate(frm);'>
</form>
</body>

A little bit of google search won't hurt lol.

Link to comment
https://forums.phpfreaks.com/topic/45883-date-validation/#findComment-223219
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.