C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » Javascript »

Javascript date validation


Posted Date: 08 Jan 2009    Resource Type: Code Snippets    Category: Javascript
Author: AbhiMember Level: Gold    
Rating: 1 out of 5Points: 10



This code shows how to validate date using javascript


// Date Validation
function checkDate(sDate)
{
var len = sDate.length;
if(sDate != "")
{
if(len > 11)
{
alert("Not a Valid Date");
return false;
}
else
{
var dateformat = /((([0][1-9]|[12][\d])|[3][01])[-\/]([0][13578]|[1][02]|(Jan|Mar|May|Jul|Aug|Oct|Dec))[-\/][1-9]\d\d\d)|((([0][1-9]|[12][\d])|[3][0])[-\/]([0][13456789]|[1][012]|(Jan|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec))[-\/][1-9]\d\d\d)|(([0][1-9]|[12][\d])[-\/]([0][2]|[2]|(Feb))[-\/][1-9]\d([02468][048]|[13579][26]))|(([0][1-9]|[12][0-8])[-\/]([0][2]|[2]|(Feb))[-\/][1-9]\d\d\d)/i
if( ! dateformat.test(sDate))
{
alert("Not a Valid Date");
return false;
}
else
{
if(len == 11)
{
var month = sDate.substring(3, 6);
var month1 = /(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)/i
if( ! month1.test(sDate))
{
alert("Not a Valid Date");
return false;
}
}
}
}
}
return true;
}




Responses

Author: srinivasa reddy tatikonda    09 Jan 2009Member Level: Silver   Points : 2
function checkDate() {

var myDayStr = document.CheckDate.formDate.value;
var myMonthStr = document.CheckDate.formMonth.value;
var myYearStr = document.CheckDate.formYear.value;
var myMonth = new Array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'); var myDateStr = myDayStr + ' ' + myMonth[myMonthStr] + ' ' + myYearStr;

/* Using form values, create a new date object
using the setFullYear function */
var myDate = new Date();
myDate.setFullYear( myYearStr, myMonthStr, myDayStr );

if ( myDate.getMonth() != myMonthStr ) {
alert( 'I\'m sorry, but "' + myDateStr + '" is NOT a valid date.' );
} else {
alert( 'Congratulations! "' + myDateStr + '" IS a valid date.' );
}

}

Best Regards

Srinivasa Reddy


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Javascript date validation  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Javascript with Regular Expression
Previous Resource: Get Scroll position
Return to Discussion Resource Index
Post New Resource
Category: Javascript


Post resources and earn money!
 
Related Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use