Posted By

cristianogois on 11/22/12


Tagged

javascript js date validation


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

cristianogois


Javascript date validation


 / Published in: JavaScript
 

Taken from http://stackoverflow.com/questions/1353684/detecting-an-invalid-date-date-instance-in-javascript

  1. /**
  2.   check date validation
  3.   @param: dateStr: string (format: dd/mm/yyyy)
  4. **/
  5.  
  6. function isDate(dateStr) {
  7. var s = dateStr.split('/'); //format 21/12/2012
  8. var d = new Date(+s[2], s[1]-1, +s[0]);
  9.  
  10. if (Object.prototype.toString.call(d) === "[object Date]") {
  11. if (!isNaN(d.getTime()) && d.getDate() == s[0] &&
  12. d.getMonth() == (s[1] - 1)) {
  13. return true;
  14. }
  15. }
  16. return false;
  17. }

Report this snippet  

You need to login to post a comment.