Wrox Programmer Forums
|
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 25th, 2005, 05:34 AM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 479
Thanks: 0
Thanked 3 Times in 3 Posts
Send a message via MSN to surendran Send a message via Yahoo to surendran
Default Date Validation

Hi,
i have a text box if any user enter date i need to validate this (dd/mm/yyyy). please give me a simple code for this.

surendran
(Anything is Possible)
__________________
surendran
(Anything is Possible)
http://www.suren.info
http://ssuren.spaces.msn.com
 
Old February 25th, 2005, 05:49 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

Code:
function CheckValidDate(sValueToTest)
{
    var cYEAR = 2, cMONTH = 1, cDAY = 0; // ### change these for other date formats 
    var bOK = true;

        // attempt to convert to a date (assume dd/mm/yyyy format)
        var dtValueToTest;
        var aDate;
        try
        {
            // ### change this for other date formats ###
            aDate = sValueToTest.split("\/");  
            dtValueToTest = new Date(aDate[cYEAR], aDate[cMONTH] - 1, aDate[cDAY]);
            if ( isNaN(dtValueToTest) )
            {
                bOK = false;
            }
        }
        catch (e)
        {
            bOK = false;
        }

        // if the date conversion succeeded, check its the actual date that was input
        // (e.g. it will accept 31/2/2003 but will change it to 3/3/2003)
        if (bOK)
        {
            // is it the same year?
            if (aDate[cYEAR] != dtValueToTest.getFullYear())
                bOK = false;

            // is it the same month?
            if (aDate[cMONTH] != 1 + dtValueToTest.getMonth())
                bOK = false;

            // is it the same day?
            if (aDate[cDAY] != dtValueToTest.getDate())
                bOK = false;
        }

    return (bOK);
}
hth
Phil
 
Old February 25th, 2005, 05:52 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

I don't think there is any simple code for this, search for "date validation" in the JavaScript forums on this site for past sugggestions. The safest way is to provide some sort of calendar control which only allows valid entries. There are many of these freely available on the web.

--

Joe (Microsoft MVP - XML)
 
Old February 25th, 2005, 07:40 AM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 479
Thanks: 0
Thanked 3 Times in 3 Posts
Send a message via MSN to surendran Send a message via Yahoo to surendran
Default

thank you

surendran
(Anything is Possible)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Date Validation aftabn10 PHP How-To 0 January 29th, 2007 10:35 AM
Date validation vacak Struts 0 November 21st, 2006 12:11 PM
Date Validation ramesh055 ASP.NET 1.0 and 1.1 Professional 1 November 15th, 2006 11:09 AM
date validation (again) crmpicco Javascript How-To 14 March 29th, 2006 06:58 PM
Date validation Raul Javascript 4 February 25th, 2004 04:04 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.