...

View Full Version : Date Validator Similar to isDate()



jason_kelly
01-13-2012, 02:44 AM
Hi There,

I really need your help.

I am aware that there is no isDate() function for javascript, however, how could one check a date against a validator to ensure that the date entered is either returned true or false

the date format that I would like to check would consist of the dd/mm/yyyy

so essentially:

isDate("12/01/2012") would return as true

isDate("N/A") = would return as false

I have searched high and low and am counting on the experts on this forum for some major help.

A huge thanks in advance and I really appreciate everything.

Cheers,

Jay

xelawho
01-13-2012, 02:58 AM
you can check that the date is in the format you want and that there are no absurd numbers (like month 13 or day 65) like this:


var d =document.getElementById('txt').value
var validformat=/^\d{2}\/\d{2}\/\d{4}$/;
if (!validformat.test(d)){
alert('please enter your date of birth in dd/mm/yyyy format')
return;
}
var day=d.split("/")[0]
var mth=d.split("/")[1]
var yr=d.split("/")[2]
var bday = new Date(yr, mth-1, day)
if ((bday.getMonth()+1!=mth)||(bday.getDate()!=day)||(bday.getFullYear()!=yr)) {
alert("Invalid Day, Month, or Year range detected. Please correct and submit again.")
return;
}

jmrker
01-13-2012, 04:37 AM
Nice check. I modified it to reduce the number of variables and add an alternate input format.


<script type="text/javascript">
function validDate() {
var ErrCnt = -1;
var ErrMsg = ['Please enter your date of birth in dd/mm/yyyy format',
'Invalid Day, Month, or Year range detected. Please correct and submit again.'];
var dt = document.getElementById('txt').value;
var validformat = /^\d{2}[\/\-]\d{2}[\/|\-]\d{4}$/;
if (!validformat.test(dt)){ ErrCnt = 0; }
var d = dt.split(/[\/|\-]/);
var bday = new Date(d[2],(d[1]-1),d[0]);
if (((bday.getMonth()+1)!=d[1])||(bday.getDate()!=d[0])||(bday.getFullYear()!=d[2])) { ErrCnt = 1; }
if (ErrCnt < 0) {
alert('Date is OK'); // for testing purposes only
return true;
} else { alert(ErrMsg[ErrCnt]); return false; }
}
</script>
Format: dd/mm/yyyy OR dd-mm-yyyy<br>
<input type="text" id="txt" value="16/08/1947">
<button onclick="validDate()">Validate Date</button>

Could also use the returned boolean values to branch to other actions.

felgall
01-13-2012, 04:41 AM
Another alternative way to do the validation:


var a, b, c, d, e;
a = "12/01/2012";
b = new Date(a);
c = y.getDate();
c = (c<10)? '0'+c : c;
d = y.getMonth() + 1;
d = (d<10)? '0'+d : d;
e = y.getFullYear();
if (a !== c+'/'+d+'/'+e) alert('invalid date');

jmrker
01-13-2012, 05:54 AM
Another alternative way to do the validation:


var a, b, c, d, e;
a = "12/01/2012";
b = new Date(a);
c = y.getDate();
c = (c<10)? '0'+c : c;
d = y.getMonth() + 1;
d = (d<10)? '0'+d : d;
e = y.getFullYear();
if (a !== c+'/'+d+'/'+e) alert('invalid date');

I'm not sure that is correct for the question posed.
OP wanted dd/mm/yyyy not mm/dd/yyyy format.
See this ...


<script type="text/javascript">
var d1 = new Date('12/01/2012');
var d2 = new Date('01/12/2012');
alert(d1.toString()+'\n'+d2.toString());
</script>

Also where does the 'y' come from in your code.
Probably should be 'b' instead.

Finally, I think the if statement compares your incorrect dd/mm/yyyy format which is mm/dd/yyyy with the new Date('mm/dd/yyyy') setting.

to the formed format of dd/mm/yyyy

Philip M
01-13-2012, 08:32 AM
<script type = "text/javascript">

function checkValidDate(yr,mmx,dd) {

if (yr <1910 || yr >2011) { // you may want to change 2011 to some other year!
alert ("Year is out of range")
return false;
}

mm = mmx-1; // remember that in Javascript date objects the months are 0-11
var nd = new Date();
nd.setFullYear(yr,mm,dd); // format YYYY,MM(0-11),DD

var ndmm = nd.getMonth();
if (ndmm != mm) {
alert (dd + "/" + mmx + "/" + yr + " is an Invalid Date!");
return false;
}
else {
alert (dd + "/" + mmx + "/" + yr + " is a Valid Date");
}
}

checkValidDate(2011,2,31) // 31st February 2011


In theory, there isn't any difference between the theory and practice. In practice, there is.

felgall
01-13-2012, 08:34 AM
I'm not sure that is correct for the question posed.
OP wanted dd/mm/yyyy not mm/dd/yyyy format.

Data passed to Date as a string is in dd/mm/ccyy format for most of the world. It only defaults to mm/dd/ccyy if you configure your operating system to use that format - which you'd only do if you live in North America as that's the only place that uses that format.

Presumably since the OP wants dd/mm format they don't live in North America and so the Date object on their computer will be expecting dd/mm - the same as it does for most people.


Also where does the 'y' come from in your code.
Probably should be 'b' instead.

Yes, I renamed the field part way through writing the code and forgot to change all those references. So all those y references should be b.

jmrker
01-13-2012, 03:04 PM
Data passed to Date as a string is in dd/mm/ccyy format for most of the world. It only defaults to mm/dd/ccyy if you configure your operating system to use that format - which you'd only do if you live in North America as that's the only place that uses that format.

Presumably since the OP wants dd/mm format they don't live in North America and so the Date object on their computer will be expecting dd/mm - the same as it does for most people.

...

I guess I did not realize that as, yes I live in NA, and you seem to be in Australia. I'll bow to your knowledge of world-wide settings. :o

felgall
01-14-2012, 01:11 AM
I guess I did not realize that as, yes I live in NA, and you seem to be in Australia. I'll bow to your knowledge of world-wide settings. :o

I just did some further testing and it seems that JavaScript does not do quite what I expected in this instance. Even testing with a date where it is obvious from the values which parts of the date are which defining a new Date in my browser passing it '14/01/2012' it set the date to 1st February 2013 instead of the correct date. So it seems that I was wrong in assuming that Date.parse would do the logical thing and assume that whichever of the two values is over 12 is the day and that if they are both under 12 that it would examine the browser and operating system settings.

So it looks like you are correct and the date would be assumed to be mm/dd/yyyy in all cases where the year doesn't come first - even where the values make it obvious that it is intended to be dd/mm/yyyy.

jmrker
01-14-2012, 01:37 AM
I just did some further testing and it seems that JavaScript does not do quite what I expected in this instance. Even testing with a date where it is obvious from the values which parts of the date are which defining a new Date in my browser passing it '14/01/2012' it set the date to 1st February 2013 instead of the correct date. So it seems that I was wrong in assuming that Date.parse would do the logical thing and assume that whichever of the two values is over 12 is the day and that if they are both under 12 that it would examine the browser and operating system settings.

So it looks like you are correct and the date would be assumed to be mm/dd/yyyy in all cases where the year doesn't come first - even where the values make it obvious that it is intended to be dd/mm/yyyy.

Thanks for the further testing and advising me that I am not just another "(The) Ugly American"



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum