Click here to Skip to main content
6,932,036 members and growing! (12,455 online)
Email Password   helpLost your password?
 
Web Development » Client side scripting » General     Intermediate License: The Code Project Open License (CPOL)

Date Validation in JavaScript

By jebarson

An article to validate dates in JavaScript.
Javascript, Windows, .NET, ASP.NET, Visual-Studio, IE6.0, IE5.5, Dev
Posted:21 Apr 2006
Views:69,872
Bookmarked:18 times
printPrint Friendly   add Share
      Discuss Discuss   Broken Article?Report  
7 votes for this article.
Popularity: 1.69 Rating: 2.00 out of 5
2 votes, 33.3%
1
1 vote, 16.7%
2
3 votes, 50.0%
3

4

5

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:

//

 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:

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:

<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)

About the Author

jebarson


Member
Working as Technical Lead in Photon Infotech Inc., MA, USA. I love programming and that too exploring into new things.

I Started my first program by building a game on GW BASIC when I was 14. The release of the game made me addicted to fame. I fell in love with programming. I will program even on the last day of my life Smile
Occupation: Architect
Location: United States United States

Other popular Client side scripting articles:

 
Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 10 of 10 (Total in Forum: 10) (Refresh)FirstPrevNext
GeneralWorks greatly... PinmemberWaleed Eissa18:30 29 Sep '08  
QuestionHow about marking 'Automatically adjust clock...' Pinmembermurillobraga11:11 18 Jun '08  
General30th feb validation Pinmemberintern19:36 8 Jun '08  
AnswerRe: 30th feb validation Pinmemberjebarson6:40 17 Jun '08  
GeneralA approach more easy to use Pinmembertiger.zeng21:22 4 Feb '07  
GeneralRe: A approach more easy to use Pinmemberjebarson22:55 10 Apr '07  
Generalcustom validator errormessage Pinmembertwinvaibhav1:21 5 Jan '07  
GeneralRe: custom validator errormessage Pinmemberjebarson12:10 5 Jan '07  
GeneralAnother solution PinmemberPeter Blum7:19 26 Apr '06  
GeneralRe: Another solution Pinmembervinodjnair1:28 12 Jun '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

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

PermaLink | Privacy | Terms of Use
Last Updated: 21 Apr 2006
Editor: Smitha Vijayan
Copyright 2006 by jebarson
Everything else Copyright © CodeProject, 1999-2010
Web09 | Advertise on the Code Project