Click here to Skip to main content
Click here to Skip to main content

Date Validation in JavaScript

, 21 Apr 2006 CPOL
Rate this:
Please Sign up or sign in to vote.
An article to validate dates in JavaScript.


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:

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

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

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:

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


This is the code for the custom validator:

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

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


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


About the Author

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,, SL, WCF, WPF and many more.

You can visit my site at

Follow me on twitter @jebarson007
Follow on   Twitter

Comments and Discussions

QuestionNice Article PinmemberSiva Hyderabad3-Apr-14 21:44 
GeneralMy vote of 4 PinmemberMd. Humayun Rashed1-Jul-12 1:53 
GeneralMy vote of 1 PinmemberJohn Kasra12-Feb-12 13:09 
GeneralMy vote of 1 Pinmemberziaalam27-Jan-12 3:21 
GeneralWorks greatly... PinmemberWaleed Eissa29-Sep-08 18:30 
QuestionHow about marking 'Automatically adjust clock...' Pinmembermurillobraga18-Jun-08 11:11 
General30th feb validation Pinmemberintern8-Jun-08 19:36 
AnswerRe: 30th feb validation Pinmemberjebarson17-Jun-08 6:40 
GeneralA approach more easy to use Pinmembertiger.zeng4-Feb-07 21:22 
GeneralRe: A approach more easy to use Pinmemberjebarson10-Apr-07 22:55 
Generalcustom validator errormessage Pinmembertwinvaibhav5-Jan-07 1:21 
GeneralRe: custom validator errormessage Pinmemberjebarson5-Jan-07 12:10 
GeneralAnother solution PinmemberPeter Blum26-Apr-06 7:19 
GeneralRe: Another solution Pinmembervinodjnair12-Jun-06 1:28 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150414.1 | Last Updated 21 Apr 2006
Article Copyright 2006 by jebarson
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid