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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

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

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  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.

×
×
  • 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.