validators.js 9.9 KB

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