validators.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. }
  29. return {VALID: true};
  30. },
  31. 'statspassword': function(elm) {
  32. if (!!$('#stats-auth-enable').attr('checked') == true) {
  33. if ($(elm).val().trim() == '') {
  34. return {VALID: false, ERROR: App.Validate.getFieldName(elm) + ' is required'};
  35. }
  36. }
  37. return {VALID: true};
  38. },
  39. 'username' : function(elm) {
  40. if ($(elm).val().trim() != '' && $(elm).val().search(/[^a-zA-Z_]+/) != -1) {
  41. return {VALID: false, ERROR: App.Validate.getFieldName(elm) + ' is invalid'};
  42. }
  43. return {VALID: true};
  44. },
  45. 'required' : function(elm) {
  46. if ($(elm).val().trim() == '') {
  47. return {VALID: false, ERROR: App.Validate.getFieldName(elm) + ' is required'};
  48. }
  49. return {VALID: true};
  50. },
  51. 'numeric': function(elm) {
  52. if ($(elm).val().trim() != '' && isNaN(parseInt($(elm).val(), 10))) {
  53. return {VALID: false, ERROR: App.Validate.getFieldName(elm) + ' is incorrect'};
  54. }
  55. return {VALID: true};
  56. },
  57. 'no-spaces': function(elm) {
  58. if ($(elm).val().trim() != '' && $(elm).val().search(/\s/) != -1) {
  59. return {VALID: false, ERROR: App.Validate.getFieldName(elm) + ' cannot contain spaces'};
  60. }
  61. return {VALID: true};
  62. },
  63. 'abc': function(elm) {
  64. if ($(elm).val().trim() != '' && $(elm).val().search(/[^a-zA-Z]+/) != -1) {
  65. return {VALID: false, ERROR: App.Validate.getFieldName(elm) + ' must contain only letters without spaces or other symbols'};
  66. }
  67. return {VALID: true};
  68. },
  69. 'email': function(elm) {
  70. if ($(elm).val().search(/^\s*[\w\-\+_]+(\.[\w\-\+_]+)*\@[\w\-\+_]+\.[\w\-\+_]+(\.[\w\-\+_]+)*\s*$/) == -1) {
  71. return {VALID: false, ERROR: App.Validate.getFieldName(elm) + ' not a valid email'};
  72. }
  73. return {VALID: true};
  74. },
  75. 'ip': function(elm) {
  76. 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) {
  77. return {VALID: false, ERROR: App.Validate.getFieldName(elm) + ' not a valid IP value'};
  78. }
  79. return {VALID: true};
  80. },
  81. 'domain': function(elm) {
  82. if ($(elm).val().trim() != '' && (/^([a-z0-9\.])*[a-z0-9][a-z0-9\-]+[a-z0-9](\.[a-z]{2,4})+$/).test($(elm).val()) == false) {
  83. return {VALID: false, ERROR: App.Validate.getFieldName(elm) + ' not a valid domain name'};
  84. }
  85. return {VALID: true};
  86. },
  87. 'ns': function(elm) {
  88. if ($(elm).val().trim() != '' && (/^([a-z0-9\.])*[a-z0-9][a-z0-9\-]+[a-z0-9](\.[a-z]{2,4})+$/).test($(elm).val()) == false) {
  89. return {VALID: false, ERROR: App.Validate.getFieldName(elm) + ' not a valid NS name'};
  90. }
  91. return {VALID: true};
  92. },
  93. 'cronminute': function(elm) {
  94. if ($(elm).val().trim() != '' && $(elm).val().search(/[^0-9\/\*-,]+/) != -1) {
  95. return {VALID: false, ERROR: App.Validate.getFieldName(elm) + ' wrong minute value'};
  96. }
  97. return {VALID: true};
  98. },
  99. 'cronhour': function(elm) {
  100. if ($(elm).val() == '*') {
  101. return {VALID: true};
  102. }
  103. if ($(elm).val().trim() != '' && $(elm).val().search(/[^0-9\/\*-,]+/) != -1) {
  104. return {VALID: false, ERROR: App.Validate.getFieldName(elm) + ' wrong hour value'};
  105. }
  106. return {VALID: true};
  107. },
  108. 'cronwday': function(elm) {
  109. if ($(elm).val() == '*') {
  110. return {VALID: true};
  111. }
  112. if ($(elm).val().trim() != '' && $(elm).val().search(/[^123456\/\*-,]+/) != -1) {
  113. return {VALID: false, ERROR: App.Validate.getFieldName(elm) + ' wrong week day value'};
  114. }
  115. return {VALID: true};
  116. },
  117. 'cronmonth': function(elm) {
  118. if ($(elm).val() == '*') {
  119. return {VALID: true};
  120. }
  121. if ($(elm).val().trim() != '' && $(elm).val().search(/[^0-9\/\*-,]+/) != -1) {
  122. return {VALID: false, ERROR: App.Validate.getFieldName(elm) + ' wrong month value'};
  123. }
  124. return {VALID: true};
  125. },
  126. 'cronday': function(elm) {
  127. if ($(elm).val() == '*') {
  128. return {VALID: true};
  129. }
  130. if ($(elm).val().trim() != '' && $(elm).val().search(/[^0-9\/\*-,]+/) != -1) {
  131. return {VALID: false, ERROR: App.Validate.getFieldName(elm) + ' wrong day value'};
  132. }
  133. return {VALID: true};
  134. }
  135. }
  136. App.Validate.form = function(world, elm)
  137. {
  138. var form_valid = true;
  139. App.Env.FormError = [];
  140. $(elm).find('select, input, textarea').each(function(i, field)
  141. {
  142. if ($(field).attr('type') == 'checkbox') {
  143. var value = $(field).attr('checked') ? 'on' : 'off';
  144. $(field).val(value);
  145. }
  146. if ($.inArray($(field).attr('name'), ['target', 'source', 'save']) != -1) {
  147. //return; // pass
  148. }
  149. else {
  150. var rules = App.Validate.getRules(field);
  151. $(rules).each(function(i, rule)
  152. {
  153. fb.log('Validate with %o %o', rule, field);
  154. if (App.Validate.Rule[rule]) {
  155. var result = App.Validate.Rule[rule](field);
  156. fb.log(result);
  157. if (result.VALID == false) {
  158. App.Env.FormError.push(result.ERROR); //$(field).attr('name') + ' is required');
  159. form_valid = false;
  160. }
  161. }
  162. })
  163. /*if ($(field).val().trim() == '' || $(field).val().trim() == '-') {
  164. App.Env.FormError.push($(field).attr('name') + ' is required');
  165. form_valid = false;
  166. }*/
  167. }
  168. });
  169. return form_valid;
  170. }
  171. App.Validate.displayFormErrors = function(world, elm)
  172. {
  173. var errors_tpl = '';
  174. $(App.Env.FormError).each(function(i, error)
  175. {
  176. var tpl = App.Templates.get('error_elm', 'general');
  177. tpl.set(':ERROR', error);
  178. errors_tpl += tpl.finalize();
  179. });
  180. var ref = $('.form-error', elm);
  181. ref.removeClass('hidden');
  182. ref.html(errors_tpl);
  183. App.Helpers.scrollTo(ref);
  184. }
  185. App.Validate.getRules = function(elm)
  186. {
  187. try {
  188. var rules_string = $(elm).attr('class');
  189. var rules = [];
  190. var rules_splitted = rules_string.split(/\s/);
  191. $(rules_splitted).each(function(i, str)
  192. {
  193. var rule = str.split('rule-');
  194. if (rule.length > 1) {
  195. rules[rules.length++] = rule[1];
  196. }
  197. });
  198. return rules;
  199. }
  200. catch(e) {
  201. return [];
  202. }
  203. }