...

View Full Version : Date Validation



ugly
02-02-2004, 11:18 PM
Hi
i am new to Java script.hv do i check that user enters correct date format. the format for accepting is 02/02/04.
if anyhelp i would appreciate

thanks

Basscyst
02-03-2004, 12:45 AM
Hello -

Hopefully this will work for you:



<html>
<head>
<script type="text/javascript">
function dateValid(field)
{
var str=document.getElementById(field).value;
var stat=0;
checkField();

function checkField()
{
for(i=0;i<str.length;i++)
{
if(i==2||i==5)
{
var chk=str.charAt(i);

if(chk!='/')
{
stat=1;
}
}

else
{
var chk=str.charAt(i);

if(isNaN(chk))
{
stat=1;
}
}
}
}

if(stat==1)
{
alert("You Must Enter The Date In This Format: mm/dd/yy");
}

}
</script>
</head>
<body>
<form id="myForm">
<input type="text" id="fDate"><input type="button" onclick="dateValid('fDate')" value="Submit">
</body>
</html>


Hope That Helps,
Basscyst

Antoniohawk
02-03-2004, 12:54 AM
There's always good ol' regular expressions.



function checkdateformat(userinput){
var dateformat = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/
return dateformat.test(userinput) //returns true or false depending on userinput
}


http://www.javascriptkit.com/javatutors/re4.shtml

Basscyst
02-03-2004, 12:57 AM
Yeah I'm still working on those - Dang that makes it much smaller. :eek:

Basscyst

Antoniohawk
02-03-2004, 01:13 AM
I haven't figured them out yet either, but I remembered reading that at javascriptkit. :p

glenngv
02-03-2004, 03:57 AM
That would only check the format but not the validity of the date. i.e. 02/31/2004



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum