Wrox Programmer Forums
|
Javascript General Javascript discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript 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 October 13th, 2004, 07:01 AM
Authorized User
 
Join Date: Oct 2004
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Default date validation

Hi!!
I'm trying to create a date validation form with javascript where it's only allowed to fyll in either today´'s date or a future date.
I would appreciate all the help I can get!!!!!
THANXS!!!!!!!!!

 
Old October 13th, 2004, 07:09 AM
Friend of Wrox
 
Join Date: Jun 2004
Posts: 449
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via MSN to r_ganesh76
Default

You can make use of the Date object in JavaScript
   var Dt = new Date(txtDate.value);
   var curDate = new Date();

   if (dt < curDate)
     alert ("Invalid Date Entered");

Regards
Ganesh
 
Old October 13th, 2004, 07:32 AM
Authorized User
 
Join Date: Oct 2004
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ok Ganesh
the problem is that I'm just getting started with JS and I totaly suck!!!!
But thnaxs for helping me out!


 
Old October 13th, 2004, 08:49 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

Here's an example of checking that a date is valid and within a given range.
Code:
<html>
<head>
<title>Date Validation Test</title>
<script language="javascript" type="text/javascript">

// this is the function to call from a button or event handler
function Validate()
{

    // construct today's date without the time element
    var dtTemp = new Date();
    var dtToday = new Date(dtTemp.getFullYear(), dtTemp.getMonth(), dtTemp.getDate());

    // check the date is OK
        // CHANGE frmTest.txtDate TO YOUR FORM AND FIELD NAMES
    if ( CheckValidDate(document.frmTest.txtDate.value, dtToday, null) )
    {
        // action if date OK
        alert("date OK");
    }
    else
    {
        // action if check failed
        alert("bad date");
    }
}

// function to check dates
// if MinDate or MaxDate not required just pass null there
function CheckValidDate(sValueToTest, dtMinDate, dtMaxDate)
{
    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;
    }

    // min value test
    if (bOK && dtMinDate)
    {
        if (dtValueToTest < dtMinDate)
        {
            bOK = false;
        }
    }

    // max value test
    if (bOK && dtMaxDate)
    {
        if (dtValueToTest > dtMaxDate)
        {
            bOK = false;
        }
    }

    return (bOK);
}
</script>
</head>
<body>
<form action="foo.htm" method="POST" id="frmTest" name="frmTest">
<input type="text" id="txtDate" name="txtDate">
<input type="button" value="Validate" onclick="Validate();">
</form>
</body>
</html>
Do you use dd.mm.yyyy date format in Sweden? If so change this line
aDate = sValueToTest.split("\/");
to
aDate = sValueToTest.split(".");

any questions just ask.
hth
Phil
 
Old October 14th, 2004, 01:24 AM
Authorized User
 
Join Date: Oct 2004
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi!!!!That was really nice of u!!!!!!!Thnxs a lot for your help!!!!Really!!!!!!
Christina

 
Old October 14th, 2004, 02:26 AM
Authorized User
 
Join Date: Oct 2004
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Default

To: pgtips.
Thanxs once again!!!
I have one more question about the date validation.
If I want to add future dates what do I have to do? This script works only for the days date.

 
Old October 14th, 2004, 03:00 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

Sorry, I don't quite understand what you mean. Are you saying you get the "bad date" message when you enter a date in the future? Or do you mean you want to check that the date input is after a certain date in the future? Or do you mean something else?
 
Old October 14th, 2004, 03:09 AM
Authorized User
 
Join Date: Oct 2004
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi! Yes I'm getting the "bad date" when I'm trying to enter tomorrows date for example.
I want to be able to enter a future date as well and get as a return the "date ok" alert!!

 
Old October 14th, 2004, 04:49 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

Strange, I get "date OK" when I enter 15/10/2004.

Have you changed the code in any way?
 
Old October 14th, 2004, 05:17 AM
Authorized User
 
Join Date: Oct 2004
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Very strange I didn't change anything..But now it's working as it should I've just tried it!!!!Thanxs a lot once again!!!
It was very kind of you to help me!!!!!!!!!






Similar Threads
Thread Thread Starter Forum Replies Last Post
Date Validation aftabn10 PHP How-To 0 January 29th, 2007 10:35 AM
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 surendran Javascript How-To 3 February 25th, 2005 07:40 AM
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.