Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

form date less than todays date

Status
Not open for further replies.

wallflower

Programmer
Nov 8, 2002
4
US
I want something like

<script language=&quot;javascript&quot;>
function final_submit()
if( document.signup_form.CARD_EXPIRE.value == &quot;&quot; )
{
alert( &quot;Please enter a valid date&quot; );
return false;
}
return true;
</script>

To check a form date before it gets swept off by the form action.
(i don't have any preconcieved ideas that the above is correct, that why I'm here, in fact it errors)
and I need the value == &quot;&quot; to be greater than today.
help
 
ah yes, the seemingly simple task of validating dates...

one quick way:

function isDate(sDate) {
return (!isNaN(new Date(sDate).getTime()));
}

this function works fine with dates like &quot;1/3/1&quot; or &quot;12/13/1999&quot; etc...however, javascript is very forgiving and will convert &quot;12/32/1999&quot; to &quot;1/1/2000&quot; for you! this can be bad, if you want to fail a date such as &quot;12/32/1999&quot; rather than have it converted at runtime.

so, what to do? ask yourself how tightly you want to control date values...then program accordingly...you'll also need to decide what format acceptable dates are to be: MMDDYY? MMDDYYYY? DDMMYYYY? MM/DD/YY? MM-DD-YYYY? you get the point!

a more complex example:
[tt]
function isDate(sDate) {
// make sure a date is given, and it has two slashes
if (!sDate.length || (sDate.indexOf(&quot;/&quot;) == sDate.lastIndexOf(&quot;/&quot;))) return false;

// split the date string into m d y tokens
var arDate = sDate.split(&quot;/&quot;);
var M = arDate[0];
var D = arDate[1];
var Y = arDate[2];

// find out if the year is a leap year
var bIsLeap = (Y % 4 == 0);

// month must be between 1 & 12
if (M < 1 || M > 12) return false;

// define day limits for each month
var arDays = ['31',(bIsLeap)?'29':'28','31','30','31','30','31','31','30','31','30','31'];

// make sure day is within range for month
if (D < 1 || D > arDays[M - 1]) return false;

return (!isNaN(new Date(sDate).getTime()));
}
[/tt]


use it like so:
[tt]
function final_submit()
if( !isDate(document.signup_form.CARD_EXPIRE.value) )
{
alert( &quot;Please enter a valid date&quot; );
return false;
}
else return true;

[/tt]

it will accept dates in this format: one or two digit month, forward slash, one or two digit day, forward slash, two or four digit year.
=========================================================
if (!succeed) try();
-jeff
 
ok, that javascript rocks. I used it on another form. Thanks.
But.....
let me be more specific and see if i can make this a little harder.
the expire date is formatted mmyy

in your above code, I call final_submit() as an Onsubmit in the <form> tag?
 
wallflower,

if you're not required to use text fields, I would use <select> lists, at least for the month.

you could document.write() the year list out too, and it will always display the next ten years:
[tt]
<html>
<script language=&quot;javascript&quot;>
function writeYears() {
sHTML = '<select name=&quot;mySelectListName&quot;>';

for (x = 0; x < 11; x++)
sHTML += '<option>' + (new Date().getFullYear() + x - 2000) + '</option>';

sHTML += '</select>';
document.write(sHTML);
}
writeYears();
</script>
</html>[/tt]

if you must use text fields, just make sure that MM is between 1 & 12, and YY is between 00 && 99. you also might want to ensure that YY is >= this year.

yes, you can call if from the form tag: <form onsubmit=&quot;return final_submit();&quot;> =========================================================
if (!succeed) try();
-jeff
 
MMYY
instead of this, if its four characters long, I want to take the fist 2 characters and set them to M and the last two and set them to Y

// split the date string into m d y tokens
var arDate = sDate.split(&quot;/&quot;);
var M = arDate[0];
var D = arDate[1];
var Y = arDate[2];


are there listgetat like features in javascript?
 
yes, you can use the substring() method of String object:

// substring([start index[,stop index]]);

myDateString = &quot;0204&quot;;

mm = myDateString.substring(0,2); // == &quot;02&quot;
yy = myDateString.substring(2); // == &quot;04&quot;

=========================================================
if (!succeed) try();
-jeff
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top