Javascript - Validate number of days in a given month


/ Published in: JavaScript
Save to your folder(s)

How would you validate the number of days in a given month?


Copy this code and paste it in your HTML
  1. function validateDate( intDay, intMonth, intYear )
  2. {
  3. return intMonth >= 1 && intMonth <= 12 && intDay > 0 && intDay <= daysInMonth( intMonth, intYear );
  4. }
  5.  
  6. function daysInMonth( intMonth, intYear )
  7. {
  8. switch ( intMonth )
  9. {
  10. case 2:
  11. return (intYear % 4 == 0 && intYear % 100) || intYear % 400 == 0 ? 29 : 28;
  12. case 4:
  13. case 6:
  14. case 9:
  15. case 11:
  16. return 30;
  17. default :
  18. return 31
  19. }
  20. }

URL: http://stackoverflow.com/questions/1433030/validate-number-of-days-in-a-given-month

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.