p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   Javascript (http://p2p.wrox.com/forumdisplay.php?f=85)
-   -   Date validation (http://p2p.wrox.com/showthread.php?t=9159)

Raul February 9th, 2004 06:27 PM

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?


Snib February 9th, 2004 06:44 PM

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

Raul February 9th, 2004 07:20 PM

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


Raul February 25th, 2004 03:10 PM

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.


Raul February 25th, 2004 04:04 PM

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;
    }
}



All times are GMT -4. The time now is 04:29 AM.

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