validators.js 7.1 KB

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