Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old February 25th, 2005, 05:34 AM
Friend of Wrox
 
Join Date: Oct 2003
Location: colombo, WP, Sri Lanka.
Posts: 481
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
Reply With Quote
  #2 (permalink)  
Old February 25th, 2005, 05:49 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , United Kingdom.
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
Reply With Quote
  #3 (permalink)  
Old February 25th, 2005, 05:52 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
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)
Reply With Quote
  #4 (permalink)  
Old February 25th, 2005, 07:40 AM
Friend of Wrox
 
Join Date: Oct 2003
Location: colombo, WP, Sri Lanka.
Posts: 481
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)
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 05:45 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.