Click here to Skip to main content
15,885,546 members
Articles / Web Development / HTML
Article

Date Validation in JavaScript

Rate me:
Please Sign up or sign in to vote.
2.64/5 (11 votes)
21 Apr 2006CPOL 184.6K   21   14
An article to validate dates in JavaScript.

Introduction

This code is developed to validate date entered in a web page. I found several other code snippets that provide a solution, but they are all vague and confusing, while this JavaScript script provides an easy method of validating a date. Check out this.

Using the code

Shown below is the core code for the date validation, written in JavaScript:

JavaScript
//
 function IsValidDate(Day,Mn,Yr){
    var DateVal = Mn + "/" + Day + "/" + Yr;
    var dt = new Date(DateVal);

    if(dt.getDate()!=Day){
        alert('Invalid Date');
        return(false);
        }
    else if(dt.getMonth()!=Mn-1){
    //this is for the purpose JavaScript starts the month from 0
        alert('Invalid Date');
        return(false);
        }
    else if(dt.getFullYear()!=Yr){
        alert('Invalid Date');
        return(false);
        }
        
    return(true);
 }
//

If you plan to add this to a custom validator in ASP.NET, then the following method will do:

The code below is written for calling the above JavaScript function:

JavaScript
function CallDateFun(sender, args){
   var d=document.getElementById("DayDropDownListCtl").value
   var m=document.getElementById("MonthDropDownListCtl").value
   var y=document.getElementById("YearDropDownListCtl").value

   if(IsValidDate(d,m,y))
        args.IsValid=true;
   else
        args.IsValid=false;
}

This is the code for the custom validator:

HTML
<asp:CustomValidator ID="CustomDateValidatorCtl" 
  runat="server"  ClientValidationFunction="CallDateFun" 
  Text="Invalid Date" ControlToValidate="DayDropDownListCtl">
</asp:CustomValidator>

Note: DayDropDownListCtl, MonthDropDownListCtl, and YearDropDownListCtl are the dropdown lists for the date, month, and year, respectively.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Microsoft Corporation
India India
I work for Microsoft on MS technologies for application development. My interests include .net, WCF, Azure, Windows Phone, ASP.net, SL, WCF, WPF and many more.

You can visit my site at http://www.jebarson.info

Follow me on twitter @jebarson007

Comments and Discussions

 
QuestionNice Article Pin
Siva Hyderabad3-Apr-14 20:44
Siva Hyderabad3-Apr-14 20:44 
GeneralMy vote of 4 Pin
Member 105108221-Jul-12 0:53
professionalMember 105108221-Jul-12 0:53 
GeneralMy vote of 1 Pin
John B Oliver12-Feb-12 12:09
John B Oliver12-Feb-12 12:09 
GeneralMy vote of 1 Pin
ziaalam27-Jan-12 2:21
ziaalam27-Jan-12 2:21 
GeneralWorks greatly... Pin
Waleed Eissa29-Sep-08 17:30
Waleed Eissa29-Sep-08 17:30 
QuestionHow about marking 'Automatically adjust clock...' Pin
murillobraga18-Jun-08 10:11
murillobraga18-Jun-08 10:11 
General30th feb validation Pin
intern8-Jun-08 18:36
intern8-Jun-08 18:36 
AnswerRe: 30th feb validation Pin
jebarson17-Jun-08 5:40
jebarson17-Jun-08 5:40 
GeneralA approach more easy to use Pin
tiger.zeng4-Feb-07 20:22
tiger.zeng4-Feb-07 20:22 
GeneralRe: A approach more easy to use Pin
jebarson10-Apr-07 21:55
jebarson10-Apr-07 21:55 
Generalcustom validator errormessage Pin
VaibhavTiparadi5-Jan-07 0:21
VaibhavTiparadi5-Jan-07 0:21 
GeneralRe: custom validator errormessage Pin
jebarson5-Jan-07 11:10
jebarson5-Jan-07 11:10 
GeneralAnother solution Pin
Peter Blum26-Apr-06 6:19
Peter Blum26-Apr-06 6:19 
GeneralRe: Another solution Pin
vinodjnair12-Jun-06 0:28
vinodjnair12-Jun-06 0:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.