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 February 9th, 2004, 06:27 PM
Authorized User
 
Join Date: Jan 2004
Posts: 37
Thanks: 0
Thanked 0 Times in 0 Posts
Default Date validation

function doDateCheck(FchIni, FchFin)
{
if (Date.parse(FchIni.value) <= Date.parse(FchFin.value))
{
alert("The dates are valid.");
}

How do I make it so if FchIni is < todays date an alert will fire? Is there an equivalent like the data() function in VB?

 
Old February 9th, 2004, 06:44 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Code:
var dt = new Date(); //get the date

var day = dt.getDate(); //get the day

var month = dt.getMonth(); //get the month

var year = dt.getYear(); //get the year

//---------------------
//From here you will have some fancy comparisons between the saved date and the current date
//---------------------

//Example:

if(year >= old_year && month >= old_month && day >= old_date)alert("WHATEVER");
HOWEVER, the above comparison will not work. You will probably end up with a few ifs, and I seem to remember something about JavaScript's month being one behind, so January is 0, February is 1, etc.

Let me know if you need a better explanation.

HTH,

----------
---Snib---
----------
 
Old February 9th, 2004, 07:20 PM
Authorized User
 
Join Date: Jan 2004
Posts: 37
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I think this works. I didnt know you had to instantiate the date first.
var dt = new Date();
if (Date.parse(FchIni.value) <= dt)
       {
        alert("Message");
        form.FchIni.focus();
        return(false);
    }

 
Old February 25th, 2004, 03:10 PM
Authorized User
 
Join Date: Jan 2004
Posts: 37
Thanks: 0
Thanked 0 Times in 0 Posts
Default

function doDateCheck(FchIni, FchFin, mydt)
{
    var dt = new Date();
    if (Date.parse(FchIni.value) >= Date.parse(FchFin.value))
    {
    alert("Start date can't be greater than end date.");
    return false;
    }
    if (Date.parse(FchIni.value) < dt)
    {
    alert("Start Date must be equal or greater than todays date" + dt);
    return false;
    }
}

When the user inputs todays date it still fires the alert. I think it has something to do with the time being included. Is there a way to cut out the time or a work around.

 
Old February 25th, 2004, 04:04 PM
Authorized User
 
Join Date: Jan 2004
Posts: 37
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I got it to work thanks!

var today = new Date()
    var day = today.getDate()
    var month = today.getMonth()+1
    var year = today.getFullYear()
    var s = "/"
    var mydate = day + s + month + s + year

    if (Date.parse(FchIni.value) >= Date.parse(FchFin.value))
    {
    alert("Start Date must be equal or greater than todays date
    return false;
    }
    if (form.FchIni.value < mydate)
    {
    alert("%#%/!");
    return false;
    }
}






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 surendran Javascript How-To 3 February 25th, 2005 07:40 AM





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