vbDoc
asked on
Date Validate Question
Hi to all,
I know that in <CFINPUT> you can validate the date (mm/dd/yyyy). But what can I use to validate just a month/year (mm/yyyy) and later I need to validate just a year (yyyy) input.
Thanks for the help, John
I know that in <CFINPUT> you can validate the date (mm/dd/yyyy). But what can I use to validate just a month/year (mm/yyyy) and later I need to validate just a year (yyyy) input.
Thanks for the help, John
guess the only way to use is to call the ONVALIDATE function in CFINPUT and write a javascript function, I mean if you dont want to change the existing code and make it work..
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Yig, can I do a IsDate (check the date) in this SCRIPT?
John
John
i am not sure isdate() will give true for a mm/yyyy format. guess need to write a javascript function which checks a valid mm and yyyy, maybe using the substring value of mm, and check if its within 1 to 12 and substring of yyyy and check if its a valid year..
this script does for dd/mm/yyyy, instead maybe we can change it to mm/yyyy format
http://developer.irt.org/script/842.htm
this script does for dd/mm/yyyy, instead maybe we can change it to mm/yyyy format
http://developer.irt.org/script/842.htm
ASKER
Thanks for the help .... John
or maybe since we are validating and not using the value in javascript, just hardcore the date as '01' constant and use the same existing function.
<SCRIPT LANGUAGE="JavaScript"><!--
function y2k(number) { return (number < 1000) ? number + 1900 : number; }
var reason = '';
function isValidDate (myDate,sep) {
// checks if date passed is in valid dd/mm/yyyy format
if (myDate.length == 10) {
if (myDate.substring(2,3) == sep && myDate.substring(5,6) == sep) {
var date = myDate.substring(0,2);
var month = myDate.substring(3,5);
var year = myDate.substring(6,10);
var test = new Date(year,month-1,date);
if (year == y2k(test.getYear()) && (month-1 == test.getMonth()) && (date == test.getDate())) {
reason = '';
return true;
}
else {
reason = 'valid format but an invalid date';
return false;
}
}
else {
reason = 'invalid spearators';
return false;
}
}
else {
reason = 'invalid length';
return false;
}
}
function tellMeIfInvalid(myDate) {
if (isValidDate(myDate,'/'))
// document.write(myDate + ' = valid date<BR>');
return (true);
else
// document.write(myDate + ' = ' + reason + '<BR>');
return (false);
}
//--></SCRIPT>
<SCRIPT>
<!--
function validatemmyyyy(form) {
Ctrl = form.inputbox1;
if (Ctrl.value == "") {
return (false);
} else
{
strDate = '01/' + Ctrl.value
tellMeIfInvalid(strDate);
return (true);
}
}
//-->
</SCRIPT>
//didn't test it
<SCRIPT LANGUAGE="JavaScript"><!--
function y2k(number) { return (number < 1000) ? number + 1900 : number; }
var reason = '';
function isValidDate (myDate,sep) {
// checks if date passed is in valid dd/mm/yyyy format
if (myDate.length == 10) {
if (myDate.substring(2,3) == sep && myDate.substring(5,6) == sep) {
var date = myDate.substring(0,2);
var month = myDate.substring(3,5);
var year = myDate.substring(6,10);
var test = new Date(year,month-1,date);
if (year == y2k(test.getYear()) && (month-1 == test.getMonth()) && (date == test.getDate())) {
reason = '';
return true;
}
else {
reason = 'valid format but an invalid date';
return false;
}
}
else {
reason = 'invalid spearators';
return false;
}
}
else {
reason = 'invalid length';
return false;
}
}
function tellMeIfInvalid(myDate) {
if (isValidDate(myDate,'/'))
// document.write(myDate + ' = valid date<BR>');
return (true);
else
// document.write(myDate + ' = ' + reason + '<BR>');
return (false);
}
//--></SCRIPT>
<SCRIPT>
<!--
function validatemmyyyy(form) {
Ctrl = form.inputbox1;
if (Ctrl.value == "") {
return (false);
} else
{
strDate = '01/' + Ctrl.value
tellMeIfInvalid(strDate);
return (true);
}
}
//-->
</SCRIPT>
//didn't test it