...

View Full Version : Verify Date function



jtelle03
12-03-2009, 08:01 PM
I'm trying to verify todays date against my input date, using a check value function, but for some reason when it comes to a day over 9 like 12/10/2009 or 12/20/2009 looks like is only recognizing the first number 12/1/2009 and 12/2/2009 and getting my alert back as to choose a future date:rolleyes:, this is my code, any help would be greatly appreciated:


var currentTime = new Date()
var month = currentTime.getMonth() + 1
var day = currentTime.getDate()
var year = currentTime.getFullYear()
function checkValues()
{
if (owt8.dayr.value <=month+'/'+day+'/'+year )
{
alert("Please enter a future date!");
return false;
}
owt8.submit();
alert("Your request has been submited")
window.close()
}

Philip M
12-03-2009, 08:08 PM
Try this:-



<script type = "text/javascript">

function checkFutureDate() {
var end_year = 2009;
var end_month = 11; // months are 0-11
var end_day = 4;

var now = new Date().getTime();
var d = new Date();
d.setFullYear(end_year, end_month, end_day); // YYYY,MM(0-11),DD
var selectedDate = d.getTime(); // today or after
if (selectedDate <= now) { // valid after today's date
//if (selectedDate < now) { // valid on today's date or after
alert ("Date must be (on or) after today's date!");
return false;
}
alert ("Date is valid");
return true;
}

checkFutureDate();

</script>


"In the beginner's mind there are many possibilities, but in the expert's mind there are few” - Shunryu Suzuki (Japanese Zen priest, ?-1971)

jtelle03
12-03-2009, 09:02 PM
I forgot to mention, my variable is "owt8.dayr.value", this is the value I'm verifying, this is a form where when the date is input, will not allow to select current date or before, the logic I have is if date is less than or equal to current date, then will give me the alert, else will submit the form.

Thanks for your help

jtelle03
12-03-2009, 10:51 PM
I guess my problem is that is not in a variable maybe?

glenngv
12-03-2009, 11:00 PM
if (new Date(owt8.dayr.value) <= new Date())
{
alert("Please enter a future date!");
return false;
}
You may want to validate first the input if it is a valid date.

jtelle03
12-03-2009, 11:07 PM
yeah, it returns the right date, I manually input it

jtelle03
12-03-2009, 11:08 PM
I actually have a little javascript calendar to format my time, but even if manually input still gives me the error.

jtelle03
12-03-2009, 11:24 PM
var currentTime = new Date()
var month = currentTime.getMonth() + 1
var day = currentTime.getDate()
var year = currentTime.getFullYear()
function checkValues()
{
if (owt8.tb1.options[0].selected == true)
{
alert("Please select your current start shift time!");
return false;
}
else if (owt8.tb2.options[0].selected == true)
{
alert("Please select your current end shift time!");
return false;
}
else if (owt8.dayr.value == "")
{
alert("Please Enter the date you are requesting!");
return false;
}
else if (new Date(owt8.dayr.value) <=new Date()) )
{
alert("Please enter a future date!");
return false;
}
owt8.submit();
alert("Your request has been submited")
window.close()
}

jtelle03
12-03-2009, 11:25 PM
this is the original:
var currentTime = new Date()
var month = currentTime.getMonth() + 1
var day = currentTime.getDate()
var year = currentTime.getFullYear()
function checkValues()
{
if (owt8.tb1.options[0].selected == true)
{
alert("Please select your current start shift time!");
return false;
}
else if (owt8.tb2.options[0].selected == true)
{
alert("Please select your current end shift time!");
return false;
}
else if (owt8.dayr.value == "")
{
alert("Please Enter the date you are requesting!");
return false;
}
else if (owt8.dayr.value <=month+'/'+day+'/'+year )
{
alert("Please enter a future date!");
return false;
}
owt8.submit();
alert("Your request has been submited")
window.close()
}

glenngv
12-03-2009, 11:38 PM
Did you not see that the browser throws an error?

You have an extra )

else if (new Date(owt8.dayr.value) <=new Date()) )

jtelle03
12-03-2009, 11:54 PM
Wow, programming is a test of patience, and attention, that WORKED, you are the man!!

Philip M
12-04-2009, 08:15 AM
Be aware that JavaScript form validation only provides convenience for users, not security. This means that JavaScript should be used as an "enhancement", not as a requirement. So your form should not be dependent on JavaScript alone to perform your validation. Instead, whatever server-side language you use to process the form (PERL, ASP, PHP, etc.) should also perform the same validation. Otherwise, people will be able to bypass your validation (and even possibly inject malicious code) simply by disabling JavaScript.

jtelle03
01-06-2010, 12:21 AM
It's me again, same code, but now I'm trying to authenticate the and copare a fix date, same code, but with a new if statement, somthing like this:

else if (new Date(owt8.dayr.value) >=Date (1/30/2010))
{
alert("No entries available on or after 1/30/2010!");
return false;


for some reason I believe 1/30/2010 is not been recognized as a date, my variable are

var currentTime = new Date()
var month = currentTime.getMonth() + 1
var day = currentTime.getDate()
var year = currentTime.getFullYear();


Thank you in advance

jtelle03
01-06-2010, 12:22 AM
var currentTime = new Date()
var month = currentTime.getMonth() + 1
var day = currentTime.getDate()
var year = currentTime.getFullYear();
function checkValues()
{
if (new Date(owt8.dayr.value) <=new Date())
{
alert("Please enter a future date!");
return false;
}
else if (new Date(owt8.dayr.value) >=Date (1/30/2010))
{
alert("No entries available on or after 1/30/2010!");
return false;
}
owt8.submit();
alert("Your request has been submited")
window.close()
}

Philip M
01-06-2010, 09:11 AM
var futureDate = new Date(2010,0,30); // 30th January 2010
......
else if (new Date(owt8.dayr.value) >= futureDate) {
alert("No entries available on or after 1/30/2010!");
return false;
}



"Kuku kaki kakak kakak ku kayak kuku kaki kakek kakek ku" is Indonesian for "My sister's toenails look like my grandfather's."

jtelle03
01-06-2010, 04:01 PM
So I get this clear, when declaring the variable:
var futureDate = new Date(2010,0,30); // 30th January 2010
will this also work?
var futureDate = new Date(1/30/2010); // 30th January 2010

?:D

Philip M
01-06-2010, 07:45 PM
So I get this clear, when declaring the variable:
var futureDate = new Date(2010,0,30); // 30th January 2010
will this also work?
var futureDate = new Date(1/30/2010); // 30th January 2010

?:D

No. The syntax is YYYY,MM,DD (remember that months are 0-11).

Have a look at how the date object works at
http://www.javascriptkit.com/jsref/date.shtml

Gjslick
01-07-2010, 04:28 AM
So I get this clear, when declaring the variable:
var futureDate = new Date(2010,0,30); // 30th January 2010
will this also work?
var futureDate = new Date(1/30/2010); // 30th January 2010

?:D

Actually, you can create a Date object from a "normal" date, like 1/30/2010. The Date object's constructor will accept a single string argument, which it will parse into a date.

Ex:

var date = new Date( '1/30/2010' );



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum