You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

195 lines
5.4 KiB

  1. var field2focus = null;
  2. function checknumber(obj) {
  3. var x = obj.value
  4. x = x.replace(",", "");
  5. x = x.replace(",", "");
  6. x = x.replace(",", "");
  7. x = x.replace(",", "");
  8. x = x.replace(",", "");
  9. if (x == '') {
  10. return false;
  11. }
  12. var anum = /(^\d+$)|(^\d+\.\d{1,10}$)/
  13. if (anum.test(x)) {
  14. return true;
  15. }
  16. else {
  17. alert("Please input a valid number!")
  18. field2focus = obj;
  19. setTimeout('focusField()', 10);
  20. return false;
  21. }
  22. }
  23. function isIntegerOnly(obj) {
  24. var i;
  25. var s = obj.value;
  26. for (i = 0; i < s.length; i++) {
  27. // Check that current character is number.
  28. var c = s.charAt(i);
  29. if (((c < "0") || (c > "9"))) {
  30. alert("Please input a valid integer!")
  31. field2focus = obj;
  32. setTimeout('focusField()', 10);
  33. return false;
  34. }
  35. }
  36. // All characters are integers.
  37. return true;
  38. }
  39. function focusField() {
  40. if (field2focus) field2focus.focus();
  41. }
  42. function isAlphanumeric(s) {
  43. var i;
  44. if (isEmpty(s))
  45. if (isAlphanumeric.arguments.length == 1) return defaultEmptyOK;
  46. else return (isAlphanumeric.arguments[1] == true);
  47. for (i = 0; i < s.length; i++) {
  48. // Check that current character is number or letter.
  49. var c = s.charAt(i);
  50. if (!(isLetter(c) || isDigit(c)))
  51. return false;
  52. }
  53. // All characters are numbers or letters.
  54. return true;
  55. }
  56. function isYear(s) {
  57. if (isEmpty(s))
  58. if (isYear.arguments.length == 1) return defaultEmptyOK;
  59. else return (isYear.arguments[1] == true);
  60. if (!isNonnegativeInteger(s)) return false;
  61. return ((s.length == 2) || (s.length == 4));
  62. }
  63. function isIntegerInRange(s, a, b) {
  64. if (isEmpty(s))
  65. if (isIntegerInRange.arguments.length == 1) return defaultEmptyOK;
  66. else return (isIntegerInRange.arguments[1] == true);
  67. if (!isInteger(s, false)) return false;
  68. var num = parseInt(s);
  69. return ((num >= a) && (num <= b));
  70. }
  71. function isMonth(s) {
  72. if (isEmpty(s))
  73. if (isMonth.arguments.length == 1) return defaultEmptyOK;
  74. else return (isMonth.arguments[1] == true);
  75. return isIntegerInRange(s, 1, 12);
  76. }
  77. function isDay(s) {
  78. if (isEmpty(s))
  79. if (isDay.arguments.length == 1) return defaultEmptyOK;
  80. else return (isDay.arguments[1] == true);
  81. return isIntegerInRange(s, 1, 31);
  82. }
  83. // Declaring valid date character, minimum year and maximum year
  84. var dtCh = "/";
  85. var minYear = 1900;
  86. var maxYear = 2100;
  87. function isInteger(s) {
  88. var i;
  89. for (i = 0; i < s.length; i++) {
  90. // Check that current character is number.
  91. var c = s.charAt(i);
  92. if (((c < "0") || (c > "9"))) return false;
  93. }
  94. // All characters are numbers.
  95. return true;
  96. }
  97. function stripCharsInBag(s, bag) {
  98. var i;
  99. var returnString = "";
  100. // Search through string's characters one by one.
  101. // If character is not in bag, append to returnString.
  102. for (i = 0; i < s.length; i++) {
  103. var c = s.charAt(i);
  104. if (bag.indexOf(c) == -1) returnString += c;
  105. }
  106. return returnString;
  107. }
  108. function daysInFebruary(year) {
  109. // February has 29 days in any year evenly divisible by four,
  110. // EXCEPT for centurial years which are not also divisible by 400.
  111. return (((year % 4 == 0) && ((!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28);
  112. }
  113. function DaysArray(n) {
  114. for (var i = 1; i <= n; i++) {
  115. this[i] = 31
  116. if (i == 4 || i == 6 || i == 9 || i == 11) { this[i] = 30 }
  117. if (i == 2) { this[i] = 29 }
  118. }
  119. return this
  120. }
  121. function isDate(dtStr) {
  122. var daysInMonth = DaysArray(12)
  123. var pos1 = dtStr.indexOf(dtCh)
  124. var pos2 = dtStr.indexOf(dtCh, pos1 + 1)
  125. var strMonth = dtStr.substring(0, pos1)
  126. var strDay = dtStr.substring(pos1 + 1, pos2)
  127. var strYear = dtStr.substring(pos2 + 1)
  128. strYr = strYear
  129. if (strDay.charAt(0) == "0" && strDay.length > 1) strDay = strDay.substring(1)
  130. if (strMonth.charAt(0) == "0" && strMonth.length > 1) strMonth = strMonth.substring(1)
  131. for (var i = 1; i <= 3; i++) {
  132. if (strYr.charAt(0) == "0" && strYr.length > 1) strYr = strYr.substring(1)
  133. }
  134. month = parseInt(strMonth)
  135. day = parseInt(strDay)
  136. year = parseInt(strYr)
  137. if (pos1 == -1 || pos2 == -1) {
  138. alert("The date format should be : mm/dd/yyyy")
  139. return false
  140. }
  141. if (strMonth.length < 1 || month < 1 || month > 12) {
  142. alert("Please enter a valid month")
  143. return false
  144. }
  145. if (strDay.length < 1 || day < 1 || day > 31 || (month == 2 && day > daysInFebruary(year)) || day > daysInMonth[month]) {
  146. alert("Please enter a valid day")
  147. return false
  148. }
  149. if (strYear.length != 4 || year == 0 || year < minYear || year > maxYear) {
  150. alert("Please enter a valid 4 digit year between " + minYear + " and " + maxYear)
  151. return false
  152. }
  153. if (dtStr.indexOf(dtCh, pos2 + 1) != -1 || isInteger(stripCharsInBag(dtStr, dtCh)) == false) {
  154. alert("Please enter a valid date")
  155. return false
  156. }
  157. return true
  158. }
  159. function ValidateForm() {
  160. var dt = document.frmSample.txtDate
  161. if (isDate(dt.value) == false) {
  162. dt.focus()
  163. return false
  164. }
  165. return true
  166. }
  167. function checkDateFormat(obj) {
  168. if (obj.value != "") {
  169. var result = isDate(obj.value);
  170. if (!result) {
  171. field2focus = obj;
  172. setTimeout('focusField()', 10);
  173. obj.focus();
  174. }
  175. }
  176. }