Guest User

Untitled

a guest
May 27th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.15 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.  
  4. <head>
  5. <meta charset="utf-8">
  6. <title>Checking Date</title>
  7. <style>
  8. li {
  9. list-style-type: none;
  10. font-size: 16pt;
  11. }
  12.  
  13. .mail {
  14. margin: auto;
  15. padding-top: 10px;
  16. padding-bottom: 10px;
  17. width: 400px;
  18. background: #D8F1F8;
  19. border: 1px soild silver;
  20. }
  21.  
  22. .mail h2 {
  23. margin-left: 38px;
  24. }
  25.  
  26. input {
  27. font-size: 17pt;
  28. }
  29.  
  30. input:focus,
  31. textarea:focus {
  32. background-color: lightyellow;
  33. }
  34.  
  35. input submit {
  36. font-size: 12pt;
  37. }
  38.  
  39. .rq {
  40. color: #FF0000;
  41. font-size: 10pt;
  42. }
  43.  
  44. #result {
  45. margin-left: 10px;
  46. }
  47. </style>
  48. </head>
  49. <body>
  50. <div class="mail">
  51. <h2>Input a date [mm/dd/yyyy or mm-dd-yyyy format]</h2>
  52. <ul>
  53. <li>
  54. <input type='text' id='input' />
  55. </li>
  56. <li>&nbsp;</li>
  57. <li class="submit">
  58. <input type="submit" name="submit" value="Validate Date" onclick="ValidateDate()" />
  59. </li>
  60. <li>&nbsp;</li>
  61. <li class="submit">
  62. <input type="submit" name="submit" value="Reformat Date" onclick="reformatDate()" />
  63. </li>
  64. <li>&nbsp;</li>
  65. </ul>
  66. <li id="result"></li>
  67. </div>
  68. <script>
  69. var matches;
  70.  
  71. function isValidDate(date) {
  72. matches = /^(\d{1,2})[-\/](\d{1,2})[-\/](\d{2,4})$/.exec(date);
  73. if (matches == null) return false;
  74. var d = matches[1];
  75. var m = matches[2] - 1;
  76. var y = matches[3];
  77. var composedDate = new Date(y, m, d);
  78. return composedDate.getDate() == d && composedDate.getMonth() == m && composedDate.getFullYear() == y;
  79.  
  80. }
  81.  
  82. function ValidateDate() {
  83. var date = document.getElementById('input').value;
  84. if (isValidDate(date)) {
  85. document.getElementById('result').innerHTML = 'Date is Valid';
  86. }
  87. else {
  88. document.getElementById('result').innerHTML = 'Date is InValid';
  89. }
  90. }
  91.  
  92. function reformatDate() {
  93. var date = document.getElementById('input').value;
  94. if (isValidDate(date)) {
  95. var res = (matches[0].indexOf('-') !== -1) ? matches[0].split("-").reverse().join("/") : matches[0].split("/").reverse().join("-");
  96. document.getElementById('result').innerHTML = 'New Format : ' + res;
  97. }
  98. else {
  99. document.getElementById('result').innerHTML = 'Date is InValid';
  100. }
  101. }
  102.  
  103. </script>
  104. </body>
  105.  
  106. </html>
Add Comment
Please, Sign In to add comment