Problem checking to see if two digit year is expir

# Thread: Problem checking to see if two digit year is expir

1. Senior Member
Join Date
Dec 1969
Posts
252

## Problem checking to see if two digit year is expir

I don&#039;t know what is wrong, but this is not working. thanks.<BR><BR>var coform = document.form1;<BR><BR> }<BR>var tmpyear;<BR><BR><BR>if (coform.Expire_Year.value &#062; 03)<BR>tmpyear = "20" + coform.Expire_Year.value;<BR>else if (coform.Expire_Year.value &#060; 21)<BR>tmpyear = "20" + coform.Expire_Year.value;<BR>else {<BR>alert("The Expiration Year is not valid.");<BR>return false;<BR> }<BR>tmpmonth = coform.ExpMon.value;<BR>if (isExpiryDate(tmpyear, tmpmonth)) {<BR>alert("This card has already expired.");<BR>return;<BR>}<BR><BR>function isExpiryDate() {<BR>var argv = isExpiryDate.arguments;<BR>var argc = isExpiryDate.arguments.length;<BR><BR>year = argc &#062; 0 ? argv[0] : this.year;<BR>month = argc &#062; 1 ? argv[1] : this.month;<BR><BR>if (!isNum(year+""))<BR>return false;<BR>if (!isNum(month+""))<BR>return false;<BR>today = new Date();<BR>expiry = new Date(year, month);<BR>if (today.getTime() &#062; expiry.getTime())<BR>return false;<BR>else<BR>return true;<BR>}<BR><BR>

2. Senior Member
Join Date
Dec 1969
Posts
96,118

## .value property is *ALWAYS* a string

So you have to convert it to a number:<BR><BR>if (parseInt( coform.Expire_Year.value ) &#062; 3 )<BR><BR>But all of this code seems bogus to me. What happens if the user *does* enter a 4 digit year??? You are going to add 20 to the front of it and you&#039;ll end up with<BR> 202005<BR><BR>Ugh.<BR><BR>And then, from the logic of your code, if somebody enters the year value 21 through 99, you will do *NOTHING* to it. I suppose that&#039;s okay for this application, but...<BR><BR>And what are "this.year" and "this.month" in there? Is this function actually a method on an object? <BR><BR>And then in your isExpiryDate() function, *why* would you use argv and argc??? If *your* code is calling this function, then you *know* there are exactly two arguments, no?<BR><BR>Truthfully, I don&#039;t see the point of isExpiryDate(). It would take no more code to plunk the test in place.<BR><BR><BR>***********<BR><BR>I&#039;d code it this way:<BR><BR>function whatever( ) <BR>{<BR> var yr = parseInt(coform.Expire_Year.value);<BR> var mn = parseInt(coform.Expire_Month.value);<BR> if ( isNaN(yr) &#124&#124 isNaN(mn) )<BR> {<BR> alert("Invalid year or month number...try again...");<BR> return false;<BR> }<BR> if ( yr &#060; 21 ) yr += 2000; // simple and effective and works!<BR><BR> if ( (new Date()) &#062; (new Data(yr,mn,1)) )<BR> {<BR> alert("Card has already expired");<BR> return false;<BR> }<BR> return true;<BR>}<BR><BR>

3. Senior Member
Join Date
Dec 1969
Posts
252

## thanks.. and i want to know what would new date(

return. I am curious what is the return value of <BR>new date(yr, mn, 1)

4. Senior Member
Join Date
Dec 1969
Posts
96,118

## new date(yr, mn,1) would give error..

But<BR> new Date( yr, mn, 1 )<BR>would give a Date object that would represent the 1st day of the month mn in the year yr. CAUTION: mn is 0 to 11 in JS code, *NOT* 1 to 12.<BR><BR>I&#039;m curious as to why you asked, considering you used it in your own code.<BR><BR>

5. Senior Member
Join Date
Dec 1969
Posts
252

## i am asking just to make sure because i am getting

all confused with all of these date functions for asp, and jscript. Thanks.

6. Senior Member
Join Date
Dec 1969
Posts
96,118

## Hmmm...I might have a bug there...

Your code compares the getTime() values.<BR><BR>So maybe this:<BR> if ( (new Date()) &#062; (new Data(yr,mn,1)) )<BR><BR>should be:<BR> if ( (new Date()).getTime() &#062; (new Data(yr,mn,1)).getTime() )<BR><BR>Well, at least the latter ought to work.<BR><BR><BR>

7. Senior Member
Join Date
Dec 1969
Posts
96,118

## I tested it...

...and it works. Try this page:<BR><BR>******** junk.html **********<BR>&#060;HTML&#062;&#060;BODY&#062;<BR> &#060;SCRIPT&#062;<BR>function checkExpiration(coform) <BR>{ <BR> var yr = parseInt(coform.Expire_Year.value); <BR> var mn = parseInt(coform.Expire_Month.value); <BR> if ( isNaN(yr) &#124&#124 isNaN(mn) ) <BR> { <BR> alert("Invalid year or month number...try again..."); <BR> return false; <BR> } <BR> if ( yr &#060; 21 ) yr += 2000; // simple and effective and works! <BR><BR> // put back year and month as we understand them<BR> coform.Expire_Year.value = yr;<BR> coform.Expire_Month.value = mn;<BR><BR> if ( new Date() &#062; new Date(yr,mn,1) ) <BR> { <BR> alert("Card has already expired"); <BR> return false; <BR> } <BR> return true; <BR>} <BR>&#060;/SCRIPT&#062;<BR><BR>&#060;FORM Name="theForm" onSubmit="return checkExpiration(this);"&#062;<BR>Month (0 to 11): &#060;INPUT Name="Expire_Month"&#062;&#060;P&#062;<BR>Year (2 or 4 digits): &#060;INPUT Name="Expire_Year"&#062;&#060;P&#062;<BR>&#060;INP UT Type=Submit&#062;<BR>&#060;/FORM&#062;<BR><BR>&#060;/BODY&#062;&#060;/HTML&#062;<BR>********************<BR><BR>Now, JS in some browser *might* need the getTime() added. It can&#039;t hurt.<BR><BR>

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•