/    Sign up×
Community /Pin to ProfileBookmark

validation for yyyymmdd

HI,

i am entering date in the format ‘yyyymmdd’ in the form.

once i click save button , it must validate the date .

any validation code ,please.

Thanks & Regards,
drams.

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@forty2Mar 24.2007 — with Date object:
<i>
</i>&lt;script type="text/javascript"&gt;
function isValidDate(year, month, date)
{
var flagDate = new Date(year, month, date);
if (year != flagDate.getFullYear() || month != flagDate.getMonth() || date != flagDate.getDate())
{
return false;
}
return true;
}
alert(isValidDate(2000, 1, 29));
alert(isValidDate(2001, 1, 28));
alert(isValidDate(2001, 1, 29));
alert(isValidDate(2007, 12, 1));
&lt;/script&gt;

Without Date object:
<i>
</i>&lt;script type="text/javascript"&gt;
function getDayInMonth(year, month)
{
if (month == 2)
{
if (year % 4 == 0 &amp;&amp; (year &lt;= 1582 || (year % 100 != 0 || year % 400 == 0)))
{
return 29;
}
return 28;
}
if (month &gt; 7) month ++;
return (month % 2 == 0) ? 30 : 31;
}


<i> </i> function isValidDate(year, month, date)
<i> </i> {
<i> </i> for (i = 0; i &lt; arguments.length; i++)
<i> </i> {
<i> </i> if (isNaN(arguments[i]) || arguments[i] &lt; 1) return false;
<i> </i> }
<i> </i> if (month &gt; 12 || date &gt; getDayInMonth(year, month)) return false;

<i> </i> return true;
<i> </i> }

<i> </i> alert(isValidDate(2000, 2, 29));
<i> </i> alert(isValidDate(2001, 4, 31));
<i> </i> alert(isValidDate(2100, 2, 29));
<i> </i> alert(isValidDate(1500, 2, 29));
&lt;/script&gt;
Copy linkTweet thisAlerts:
@RMirandaMay 03.2012 — forty2, there's one problem with your logic, javascript month in Date Object goes from 0(Jan)-11(Dec) extremely stupid i know, so it should be:

(also added parseInt for protection)

<script type="text/javascript">

function isValidDate(year, month, date)

{

[COLOR="Red"]var flagDate = new Date(parseInt(year, 10), parseInt(month, 10) - 1, parseInt(date, 10)); [/COLOR]

if (year != flagDate.getFullYear() || month != flagDate.getMonth()[COLOR="Red"]+1[/COLOR] || date != flagDate.getDate())

{

return false;

}

return true;

}

alert(isValidDate(2000, 1, 29));

alert(isValidDate(2001, 1, 28));

alert(isValidDate(2001, 1, 29));

alert(isValidDate(2007, 12, 1));

</script>

PS: You should edit your post because this is thread is the one that pops out first in a google search ?
×

Success!

Help @drams spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...