Do any of you know a way or any reserved function in Javascript to validate whether a date is valid or not? For eg : 02/30/2001 is not a valid date. Date formate preferble is mm/dd/yyyy but not necessary.
All help is appreciated.
You probably got no response because this is a pretty extensive program in JS.
Here is some code that I found.
[tt]
function isValidDate(date2check){
// dgk-10/07/00
// determines if the date string passed represents a valid date.
// returns 0 if the date is valid
// returns 1 if the date is not in the format of mm/dd/ccyy, mm-dd-ccyy, or mmddccyy
// m/d/ccyy & m-d-ccyy are also acceptable
// returns 2 if the date is not a legal date (i.e. 02/30/1999)
var retval = 0
var aMMDDCCYY
var dtest
// use a regular expression pattern match to determine if the date format is valid
if (/^(\d\d?-\d\d?-\d{4})|(\d\d?\/\d\d?\/\d{4})|(\d{8})$/.test(date2check)){
dtest = new Date(date2check);
if (/\//.test(date2check)){
aMMDDCCYY = date2check.split("/"
}else{
if (/-/.test(date2check)){
aMMDDCCYY = date2check.split("-"
}else{
aMMDDCCYY = Array(date2check.substr(0,2), date2check.substr(2,2), date2check.substr(4,4))
dtest = new Date(aMMDDCCYY[0] + "/" + aMMDDCCYY[1] + "/" + aMMDDCCYY[2]);
}
}
if (dtest.getMonth() + 1 != aMMDDCCYY[0] || dtest.getDate() != aMMDDCCYY[1] || dtest.getFullYear() != aMMDDCCYY[2]){
retval = 2
}
}else{
retval = 1
}
return retval
}
function StdDateFormat(strDate){
// dgk-10/07/00
// receives a date string in the format of mm/dd/ccyy, mm-dd-ccyy, or mmddccyy
// returns a date string in the standard format: mm/dd/ccyy.
var retval = ""
var aMMDDCCYY
if (isValidDate(strDate) == 0){
if (/\//.test(strDate)){
retval = strDate;
}else{
if (/-/.test(strDate)){
aMMDDCCYY = strDate.split("-"
}else{
aMMDDCCYY = Array(strDate.substr(0,2), strDate.substr(2,2), strDate.substr(4,4));
}
retval = (aMMDDCCYY[0] + "/" + aMMDDCCYY[1] + "/" + aMMDDCCYY[2]);
}
}
return retval;
}
[/tt]
Oh o........I wanted to know if there was any reserved function..u know kinda in-built....I know how to code it...am lazy since why code if we already have a built in function - know what I mean?
I was about to ask the forum for a robust way to validate date values. I'll have to give your post a try.
To the rest out there. I'm a newbie at REs... is there a good concise online resource that explains REs? I'm currently developing in IIS5.0 with WinServer2k running my pages with both ASP/VBscript and Javascript...
Simple strict format date field validation. You are limited to one format of date entry: dd/mm/yyyy. yy can be entered it will be altered to yyyy with the following rule: yy <= 10 is += 2000 / yy > 10 += 1900
Any non word delimeter can be entered but will be changed to "/"
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.