validators.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. App.Validate.getFieldName = function(elm)
  2. {
  3. var txt_label = $(elm).prev('label').text();
  4. if (txt_label.trim() == '') {
  5. txt_label = $(elm).parents('.field-box').select('label:first').text();
  6. }
  7. return ['<strong>', txt_label, '</strong>'].join('');
  8. }
  9. App.Validate.Rule = {
  10. 'alias' : function(elm) {
  11. if (!!$('#stats-auth-enable').attr('checked') == true) {
  12. if ($(elm).val().trim() == '' || $(elm).val().search(/[^a-zA-Z\.\-\*\,\ ]+/) != -1) {
  13. return {VALID: false, ERROR: App.Validate.getFieldName(elm) + ' is invalid'};
  14. }
  15. if ($(elm).val().trim() != '' && $(elm).val().length > App.Settings.FIELD_MAX_LEN) {
  16. return {VALID: false, ERROR: App.Validate.getFieldName(elm) + ' too long'};
  17. }
  18. }
  19. return {VALID: true};
  20. },
  21. 'statslogin' : function(elm) {
  22. if (!!$('#stats-auth-enable').attr('checked') == true) {
  23. if ($(elm).val().trim() == '' || $(elm).val().search(/[^a-zA-Z_0-9]+/) != -1) {
  24. return {VALID: false, ERROR: App.Validate.getFieldName(elm) + ' is invalid'};
  25. }
  26. if ($(elm).val().trim() != '' && $(elm).val().length > App.Settings.FIELD_MAX_LEN) {
  27. return {VALID: false, ERROR: App.Validate.getFieldName(elm) + ' too long'};
  28. }
  29. }
  30. return {VALID: true};
  31. },
  32. 'statspassword': function(elm) {
  33. if (!!$('#stats-auth-enable').attr('checked') == true) {
  34. if ($(elm).val().trim() == '') {
  35. return {VALID: false, ERROR: App.Validate.getFieldName(elm) + ' is required'};
  36. }
  37. if ($(elm).val().trim() != '' && $(elm).val().length > App.Settings.FIELD_MAX_LEN) {
  38. return {VALID: false, ERROR: App.Validate.getFieldName(elm) + ' too long'};
  39. }
  40. if ($(elm).val().trim() != '' && $(elm).val().length < App.Settings.PSW_MIN_LEN) {
  41. return {VALID: false, ERROR: App.Validate.getFieldName(elm) + ' too short'};
  42. }
  43. }
  44. return {VALID: true};
  45. },
  46. 'username' : function(elm) {
  47. if ($(elm).val().trim() != '' && $(elm).val().search(/[^a-zA-Z_0-9\.\-]+/) != -1) {
  48. return {VALID: false, ERROR: App.Validate.getFieldName(elm) + ' is invalid'};
  49. }
  50. if ($(elm).val().trim() != '' && $(elm).val().length > App.Settings.FIELD_MAX_LEN) {
  51. return {VALID: false, ERROR: App.Validate.getFieldName(elm) + ' too long'};
  52. }
  53. return {VALID: true};
  54. },
  55. 'required' : function(elm) {
  56. if ($(elm).val().trim() == '') {
  57. return {VALID: false, ERROR: App.Validate.getFieldName(elm) + ' is required'};
  58. }
  59. return {VALID: true};
  60. },
  61. 'numeric': function(elm) {
  62. if ($(elm).val().trim() != '' && isNaN(parseInt($(elm).val(), 10))) {
  63. return {VALID: false, ERROR: App.Validate.getFieldName(elm) + ' is incorrect'};
  64. }
  65. return {VALID: true};
  66. },
  67. 'no-spaces': function(elm) {
  68. if ($(elm).val().trim() != '' && $(elm).val().search(/\s/) != -1) {
  69. return {VALID: false, ERROR: App.Validate.getFieldName(elm) + ' cannot contain spaces'};
  70. }
  71. return {VALID: true};
  72. },
  73. 'abc': function(elm) {
  74. if ($(elm).val().trim() != '' && $(elm).val().search(/[^a-zA-Z]+/) != -1) {
  75. return {VALID: false, ERROR: App.Validate.getFieldName(elm) + ' must contain only letters without spaces or other symbols'};
  76. }
  77. if ($(elm).val().trim() != '' && $(elm).val().length > App.Settings.FIELD_MAX_LEN) {
  78. return {VALID: false, ERROR: App.Validate.getFieldName(elm) + ' too long'};
  79. }
  80. return {VALID: true};
  81. },
  82. 'email': function(elm) {
  83. if ($(elm).val().search(/^\s*[\w\-\+_]+(\.[\w\-\+_]+)*\@[\w\-\+_]+\.[\w\-\+_]+(\.[\w\-\+_]+)*\s*$/) == -1) {
  84. return {VALID: false, ERROR: App.Validate.getFieldName(elm) + ' not a valid email'};
  85. }
  86. if ($(elm).val().trim() == '' && $(elm).val().length > App.Settings.FIELD_MAX_LEN) {
  87. return {VALID: false, ERROR: App.Validate.getFieldName(elm) + ' too long'};
  88. }
  89. return {VALID: true};
  90. },
  91. 'ip': function(elm) {
  92. if ($(elm).val().trim() != '' && (/^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/).test($(elm).val()) == false) {
  93. return {VALID: false, ERROR: App.Validate.getFieldName(elm) + ' not a valid IP value'};
  94. }
  95. return {VALID: true};
  96. },
  97. 'domain': function(elm) {
  98. if ($(elm).val().trim() != '' && $(elm).val().search(/[^a-zA-Z0-9\.\-\*]+/) != -1) {
  99. return {VALID: false, ERROR: App.Validate.getFieldName(elm) + ' not a valid domain name'};
  100. }
  101. return {VALID: true};
  102. },
  103. 'ns': function(elm) {
  104. if ($(elm).val().trim() != '' && $(elm).val().search(/[^a-zA-Z0-9\.\-\*]+/) != -1) {
  105. return {VALID: false, ERROR: App.Validate.getFieldName(elm) + ' not a valid NS name'};
  106. }
  107. if ($(elm).val().trim() == '' && $(elm).val().length > App.Settings.FIELD_MAX_LEN) {
  108. return {VALID: false, ERROR: App.Validate.getFieldName(elm) + ' too long'};
  109. }
  110. return {VALID: true};
  111. },
  112. 'cronminute': function(elm) {
  113. if ($(elm).val().trim() != '' && $(elm).val().search(/[^0-9\/\*-,]+/) != -1) {
  114. return {VALID: false, ERROR: App.Validate.getFieldName(elm) + ' wrong minute value'};
  115. }
  116. if ($(elm).val().trim() == '' && $(elm).val().length > App.Settings.MINUTE_MAX_LEN) {
  117. return {VALID: false, ERROR: App.Validate.getFieldName(elm) + ' invalid'};
  118. }
  119. return {VALID: true};
  120. },
  121. 'cronhour': function(elm) {
  122. if ($(elm).val() == '*') {
  123. return {VALID: true};
  124. }
  125. if ($(elm).val().trim() != '' && $(elm).val().search(/[^0-9\/\*-,]+/) != -1) {
  126. return {VALID: false, ERROR: App.Validate.getFieldName(elm) + ' wrong hour value'};
  127. }
  128. if ($(elm).val().trim() == '' && $(elm).val().length > App.Settings.HOURS_MAX_LEN) {
  129. return {VALID: false, ERROR: App.Validate.getFieldName(elm) + ' invalid'};
  130. }
  131. return {VALID: true};
  132. },
  133. 'cronwday': function(elm) {
  134. if ($(elm).val() == '*') {
  135. return {VALID: true};
  136. }
  137. if ($(elm).val().trim() != '' && $(elm).val().search(/[^123456\/\*-,]+/) != -1) {
  138. return {VALID: false, ERROR: App.Validate.getFieldName(elm) + ' wrong week day value'};
  139. }
  140. if ($(elm).val().trim() == '' && $(elm).val().length > App.Settings.WDAY_MAX_LEN) {
  141. return {VALID: false, ERROR: App.Validate.getFieldName(elm) + ' invalid'};
  142. }
  143. return {VALID: true};
  144. },
  145. 'cronmonth': function(elm) {
  146. if ($(elm).val() == '*') {
  147. return {VALID: true};
  148. }
  149. if ($(elm).val().trim() != '' && $(elm).val().search(/[^0-9\/\*-,]+/) != -1) {
  150. return {VALID: false, ERROR: App.Validate.getFieldName(elm) + ' wrong month value'};
  151. }
  152. if ($(elm).val().trim() == '' && $(elm).val().length > App.Settings.MONTH_MAX_LEN) {
  153. return {VALID: false, ERROR: App.Validate.getFieldName(elm) + ' invalid'};
  154. }
  155. return {VALID: true};
  156. },
  157. 'cronday': function(elm) {
  158. if ($(elm).val() == '*') {
  159. return {VALID: true};
  160. }
  161. if ($(elm).val().trim() != '' && $(elm).val().search(/[^0-9\/\*-,]+/) != -1) {
  162. return {VALID: false, ERROR: App.Validate.getFieldName(elm) + ' wrong day value'};
  163. }
  164. if ($(elm).val().trim() == '' && $(elm).val().length > App.Settings.DAY_MAX_LEN) {
  165. return {VALID: false, ERROR: App.Validate.getFieldName(elm) + ' invalid'};
  166. }
  167. return {VALID: true};
  168. }
  169. }
  170. App.Validate.form = function(world, elm)
  171. {
  172. var form_valid = true;
  173. App.Env.FormError = [];
  174. $(elm).find('select, input, textarea').each(function(i, field)
  175. {
  176. if ($(field).attr('type') == 'checkbox') {
  177. var value = $(field).attr('checked') ? 'on' : 'off';
  178. $(field).val(value);
  179. }
  180. if ($.inArray($(field).attr('name'), ['target', 'source', 'save']) != -1) {
  181. //return; // pass
  182. }
  183. else {
  184. var rules = App.Validate.getRules(field);
  185. $(rules).each(function(i, rule)
  186. {
  187. fb.log('Validate with %o %o', rule, field);
  188. if (App.Validate.Rule[rule]) {
  189. var result = App.Validate.Rule[rule](field);
  190. fb.log(result);
  191. if (result.VALID == false) {
  192. App.Env.FormError.push(result.ERROR); //$(field).attr('name') + ' is required');
  193. form_valid = false;
  194. }
  195. }
  196. })
  197. /*if ($(field).val().trim() == '' || $(field).val().trim() == '-') {
  198. App.Env.FormError.push($(field).attr('name') + ' is required');
  199. form_valid = false;
  200. }*/
  201. }
  202. });
  203. return form_valid;
  204. }
  205. App.Validate.displayFormErrors = function(world, elm)
  206. {
  207. var errors_tpl = '';
  208. $(App.Env.FormError).each(function(i, error)
  209. {
  210. var tpl = App.Templates.get('error_elm', 'general');
  211. tpl.set(':ERROR', error);
  212. errors_tpl += tpl.finalize();
  213. });
  214. var ref = $('.form-error', elm);
  215. ref.removeClass('hidden');
  216. ref.html(errors_tpl);
  217. App.Helpers.scrollTo(ref);
  218. }
  219. App.Validate.getRules = function(elm)
  220. {
  221. try {
  222. var rules_string = $(elm).attr('class');
  223. var rules = [];
  224. var rules_splitted = rules_string.split(/\s/);
  225. $(rules_splitted).each(function(i, str)
  226. {
  227. var rule = str.split('rule-');
  228. if (rule.length > 1) {
  229. rules[rules.length++] = rule[1];
  230. }
  231. });
  232. return rules;
  233. }
  234. catch(e) {
  235. return [];
  236. }
  237. }