PDA

View Full Version : date validation


franches
09-21-2004, 02:53 AM
hi,
i'm a newbie in javascript. could someone examine my code. The code checks if the user entered a date advance with the current date. The date today is september 21, 2004 if the user pressed october 21, 2004 then a message will display I'm sorry, Logging in advance is not allowed. When I test my code it always display Congratulations eventhough i tried september 21, 2005.

thanks. hope someone would help me.

<html>
<head>
</head>
<body>

<script language="javascript">

function checkdate()
{
var d = new Date() ;
var mydate=d.getDate() + (d.getMonth()+1) + d.getFullYear();

var myDayStr = document.CheckDate.formDate.value;
var myMonthStr = document.CheckDate.formMonth.value;
var myYearStr = document.CheckDate.formYear.value;
var myDateStr = myDayStr + myMonthStr + myYearStr;

if ( myDateStr > mydate ) {
alert( 'I\'m sorry, Logging in advance is not allowed' );
} else {
alert( 'Congratulations!' );
}


}
</script>

<FORM NAME=CheckDate METHOD=POST >

<SELECT NAME=formMonth id=formMonth onchange="checkdate()">
<OPTION VALUE=Jan>Jan
<OPTION VALUE=Feb>Feb
<OPTION VALUE=Mar>Mar
<OPTION VALUE=Apr>Apr
<OPTION VALUE=May>May
<OPTION VALUE=Jun>Jun
<OPTION VALUE=Jul>Jul
<OPTION VALUE=Aug>Aug
<OPTION VALUE=Sep>Sep
<OPTION VALUE=Oct>Oct
<OPTION VALUE=Nov>Nov
<OPTION VALUE=Dec>Dec
</SELECT>

<SELECT NAME=formDate id=formDate onchange="checkdate()">
<OPTION VALUE=1>1
<OPTION VALUE=2>2
<OPTION VALUE=3>3
<OPTION VALUE=4>4
<OPTION VALUE=5>5
<OPTION VALUE=6>6
<OPTION VALUE=7>7
<OPTION VALUE=8>8
<OPTION VALUE=9>9
<OPTION VALUE=10>10
<OPTION VALUE=11>11
<OPTION VALUE=12>12
<OPTION VALUE=13>13
<OPTION VALUE=14>14
<OPTION VALUE=15>15
<OPTION VALUE=16>16
<OPTION VALUE=17>17
<OPTION VALUE=18>18
<OPTION VALUE=19>19
<OPTION VALUE=20>20
<OPTION VALUE=21>21
<OPTION VALUE=22>22
<OPTION VALUE=23>23
<OPTION VALUE=24>24
<OPTION VALUE=25>25
<OPTION VALUE=26>26
<OPTION VALUE=27>27
<OPTION VALUE=28>28
<OPTION VALUE=29>29
<OPTION VALUE=30>30
<OPTION VALUE=31>31
</SELECT>

<SELECT NAME=formYear id=formYear onchange="checkdate()">
<OPTION VALUE=1980>2004
<OPTION VALUE=1980>2005
<OPTION VALUE=1980>2006
</SELECT>

<FORM>
</body>
</html>

fci
09-21-2004, 03:01 AM
when you read a form value it is seen as a string...
alert(typeof document.CheckDate.formYear.value);
so what you want to do is convert it to a number..
var blah = new Number(typeof document.CheckDate.formYear.value);
alert(typeof blah);
var blah = new Number('string');
alert(typeof blah);

so usually you would use isNaN to validate but since the values inputted are restricted to select menus then it should be ok

franches
09-21-2004, 03:50 AM
I'm sorry. could you explain it further? i may sound so stupid actually. if you're patient enough could explain it in a manner that i would easily get it. i'm totally new to javascript. hope you understand.

thanks.

fci
09-21-2004, 07:25 AM
Do you know any languages?

anyway...

when you read a form value it is seen as a string...
using typeof, it will tell you what type the variable is.
alert(typeof document.CheckDate.formYear.value);
if you tested the above code you would've seen that it alerted the value 'string' because in JavaScript all form values are seen as strings.

so what you want to do is convert it to a number.. which could be considered as 'casting' but it's not quite the same.
var blah = new Number(typeof document.CheckDate.formYear.value);
alert(typeof blah);
now you will see that the value 'number' is alerted since it was sucessfully converted to a number.


var blah = new Number('string');
alert(typeof blah);

now this would alert the value 'NaN' which means Not a Number.
if you did this:
alert(isNaN(blah));
it would return false if the variable's typeof != number

so usually you would use isNaN to validate but since the values inputted are restricted to select menus then all the values are considered strings and will have to be converted to using the example above.

further information about....
typeof - http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/ops.html#1042603
isNaN - http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/toplev.html#1064024

glenngv
09-21-2004, 02:09 PM
so what you want to do is convert it to a number..

You can't validate a date by just validating the date parts as numbers. The input may be all numbers but you can't be sure if it's a valid date or not.

function checkdate(oMonth, oDate, oYear)
{
var mo = oMonth.options[oMonth.selectedIndex].value;
var dt = oDate.options[oDate.selectedIndex].value;
var yr = oMonth.options[oYear.selectedIndex].value;
var today = new Date();
var myDate = new Date(yr, mo, dt);

if (myDate > today) {
alert( 'I\'m sorry, Logging in advance is not allowed' );
}
else {
alert( 'Congratulations!' );
}

...
<select name="formMonth" id="formMonth" onchange="checkdate(this, this.form.formDate, this.form.formYear)">
<option value="0">Jan</option>
<option value="1">Feb</option>
<option value="2">Mar</option>
<option value="3">Apr</option>
<option value="4">May</option>
<option value="5">Jun</option>
<option value="6">Jul</option>
<option value="7">Aug</option>
<option value="8">Sep</option>
<option value="9">Oct</option>
<option value="10">Nov</option>
<option value="11">Dec</option>
</select>
<select name="formDate" id="formDate" onchange="checkdate(this.form.formMonth, this, this.form.formYear)">
...
<select name="formYear" id="formYear" onchange="checkdate(this.form.formMonth, this.form.formDate, this)">
...

fci
09-21-2004, 02:38 PM
You can't validate a date by just validating the date parts as numbers. The input may be all numbers but you can't be sure if it's a valid date or not.


yay for me misinterpreting his post and never personally using date().

franches
09-23-2004, 07:35 AM
thanks guys. the solution to my problem is this.

function checkdate()
{
var myYear = document.CheckDate.formYear.value;
var myMonth = document.CheckDate.formMonth.value;
var myDay = document.CheckDate.formDate.value;
var myDate = myYear + "," + myMonth + "," + myDay;
DateSplit=myDate.split(',')

ToDay=new Date();
YourDate=new Date(DateSplit[0],DateSplit[1]-1,DateSplit[2])

if (YourDate>ToDay){
alert('Sorry, logging in advance is not allowed!');
}
else
{
alert ('congrats');
}
}

glenngv
09-23-2004, 07:45 AM
Why did you combine them and then split up back again? :confused:
Have you tried my suggestion? My suggestion uses the selected value directly to form a Date object. And I used the cross-browser way of getting the value of selected item in a combobox.

franches
09-24-2004, 01:33 AM
is this what you mean that i would do? when i tried this code it never displays any messages such as congratulations or sorry ....

function checkdate()
{
var myYear = document.CheckDate.formYear.value;
alert(typeof document.CheckDate.formYear.value);
var newYear = new Number(typeof document.CheckDate.formYear.value);
alert(typeof newYear);
alert (isNaN(newYear));

var myMonth = document.CheckDate.formMonth.value;
alert(typeof document.CheckDate.formMonth.value);
var newMonth = new Number(typeof document.CheckDate.formMonth.value);
alert (typeof newMonth);
alert (isNaN(newYear));

var myDay = document.CheckDate.formDate.value;
alert(typeof document.CheckDate.formDate.value);
var newDate = new Number(typeof document.CheckDate.formDate.value);
alert (typeof newMonth);
alert (isNaN(newYear));

var myDate = newYear + "," + newMonth + "," + newDay;
alert (typeof myDate);
alert (isNa(myDate));


ToDay=new Date();
alert (typeof ToDay);
alert (isNaN(ToDay));


if (myDate>ToDay){
alert('Sorry, logging in advance is not allowed!');
}
else
{
alert ('congrats');
}
}

glenngv
09-24-2004, 02:45 AM
That's not what I meant. I posted the code in my previous post. Look for it in the above posts, I only posted the code once in this thread.