Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.95 KB | None | 0 0
  1. function () {
  2. let validYear = false;
  3. let validMonth = false;
  4. let validDay = false;
  5.  
  6. let outputDiv = document.getElementById('output');
  7.  
  8. let btn = document.getElementsByTagName('button')[0];
  9. btn.addEventListener('click', validate);
  10.  
  11. let month = [
  12. "January", "February", "March",
  13. "April", "May", "June", "July",
  14. "August", "September", "October",
  15. "November", "December"
  16. ];
  17.  
  18. function validate() {
  19. let inputYear = document.getElementById('year').value;
  20. let inputMonth = document.getElementById('month').value;
  21. let inputDay = document.getElementById('day').value;
  22. let outputMonth;
  23. //validating year
  24. if (inputYear > 0 && inputYear <= 9999) {
  25. validYear = true;
  26. }
  27. // validating month
  28. if (month.includes(inputMonth)) {
  29. validMonth = true;
  30.  
  31. // format month for output
  32. outputMonth = month.indexOf(inputMonth) + 1;
  33. outputMonth = ('0' + outputMonth).slice(-2);
  34.  
  35. // validating day
  36. if (((+inputYear % 4 == 0) && (+inputYear % 100 != 0)) || (+inputYear % 400 == 0) && inputMonth === 'February') {
  37. if (inputDay > 0 && inputDay < 30) {
  38. validDay = true;
  39. }
  40. }
  41. switch (inputMonth) {
  42. case 'January':
  43. case 'March':
  44. case 'May':
  45. case 'July':
  46. case 'August':
  47. case 'October':
  48. case 'December':
  49. if (inputDay > 0 && inputDay < 32) {
  50. validDay = true;
  51. }
  52. break;
  53. case 'April':
  54. case 'Juny':
  55. case 'September':
  56. case 'November':
  57. if (inputDay > 0 && inputDay < 31) {
  58. validDay = true;
  59. }
  60. break;
  61. }
  62. }
  63. else{
  64. outputMonth = inputMonth;
  65. }
  66.  
  67. // format day
  68. inputDay = ('0' + inputDay).slice(-2);
  69.  
  70. // output
  71. if (validDay && validMonth && validYear) {
  72. let p = document.createElement('p');
  73. p.textContent = `Date: ${inputYear}-${outputMonth}-${inputDay} is valid`;
  74. outputDiv.appendChild(p);
  75. }
  76. else {
  77. let p = document.createElement('p');
  78. p.textContent = `Date: ${inputYear}-${outputMonth}-${inputDay} is not valid`;
  79. outputDiv.appendChild(p);
  80. }
  81. validYear = false;
  82. validMonth = false;
  83. validDay = false;
  84. inputYear = document.getElementById('year').value = '';
  85. inputMonth = document.getElementById('month').value = '';
  86. inputDay = document.getElementById('day').value = '';
  87. }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement