BioTorrents.de’s version of Gazelle
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.

validate.js 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. //'use strict';
  2. var elemStyles = Array();
  3. var errorElems = Array();
  4. /**
  5. * validEmail
  6. */
  7. function validEmail(str) {
  8. if (str.match(/^[_a-z0-9-]+([.+][_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/i)) {
  9. return true;
  10. } else {
  11. return false;
  12. }
  13. }
  14. /**
  15. * validLink
  16. */
  17. function validLink(str) {
  18. if (str.match(/^(https?):\/\/([a-z0-9\-\_]+\.)+([a-z]{1,5}[^\.])(\/[^<>]+)*$/i)) {
  19. return true;
  20. } else {
  21. return false;
  22. }
  23. }
  24. /**
  25. * isNumeric
  26. */
  27. function isNumeric(str, usePeriod) {
  28. matchStr = '/[^0-9';
  29. if (usePeriod) {
  30. matchStr += '\.';
  31. }
  32. matchStr = ']/';
  33. if (str.match(matchStr)) {
  34. return false;
  35. }
  36. return true;
  37. }
  38. /**
  39. * validDate
  40. */
  41. function validDate(theDate) {
  42. days = 0;
  43. theDate = theDate.split("/");
  44. month = theDate[0];
  45. day = theDate[1];
  46. year = theDate[2];
  47. if (!isNumeric(month) || !isNumeric(day) || !isNumeric(year)) { return false; }
  48. if (month == 1) { days = 31; }
  49. else if (month == 2) {
  50. if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
  51. days = 29;
  52. } else {
  53. days = 28;
  54. }
  55. }
  56. else if (month == 3) { days = 31; }
  57. else if (month == 4) { days = 30; }
  58. else if (month == 5) { days = 31; }
  59. else if (month == 6) { days = 30; }
  60. else if (month == 7) { days = 31; }
  61. else if (month == 8) { days = 31; }
  62. else if (month == 9) { days = 30; }
  63. else if (month == 10) { days = 31; }
  64. else if (month == 11) { days = 30; }
  65. else if (month == 12) { days = 31; }
  66. if (day > days || day == undefined || days == undefined || month == undefined || year == undefined || year.length < 4) {
  67. return false;
  68. } else {
  69. return true;
  70. }
  71. }
  72. /**
  73. * showError
  74. */
  75. function showError(fields, alertStr) {
  76. var tField = Array();
  77. var obj, el;
  78. if (typeof (fields) == 'object') {
  79. tField[0] = fields;
  80. } else {
  81. tField = fields.split(',');
  82. }
  83. for (s = 0; s <= tField.length - 1; s++) {
  84. obj = $('#' + tField[s]);
  85. if (obj) {
  86. el = obj.raw();
  87. obj.add_class("elem_error");
  88. if (s == 0) {
  89. el.focus();
  90. try {
  91. el.select();
  92. } catch (error) {
  93. }
  94. }
  95. var evtType = el.type == "select-one" ? "change" : "keypress";
  96. obj.listen(evtType, clearElemError);
  97. errorElems.push(tField[s]);
  98. }
  99. }
  100. if (alertStr != "") {
  101. alert(alertStr);
  102. }
  103. return false;
  104. }
  105. /**
  106. * clearErrors
  107. */
  108. function clearErrors(theForm) {
  109. elementList = document.forms[theForm].elements;
  110. for (x = 0; x <= elementList.length - 1; x++) {
  111. if (elementList[x].type != "submit" && elementList[x].type != "button") {
  112. if (!elemStyles[elementList[x].id]) {
  113. elemStyles[elementList[x].id] = elementList[x].className;
  114. }
  115. try {
  116. elementList[x].className = elemStyles[elementList[x].id];
  117. } catch (error) { }
  118. }
  119. }
  120. }
  121. /**
  122. * clearElemError
  123. */
  124. function clearElemError(evt) {
  125. var obj, el;
  126. for (x = 0; x <= errorElems.length - 1; x++) {
  127. obj = $('#' + errorElems[x]);
  128. el = obj.raw();
  129. var evtType = el.type == "select-one" ? "change" : "keypress";
  130. obj.unbind(evtType, clearElemError);
  131. el.className = elemStyles[el.id];
  132. }
  133. errorElems = Array();
  134. }