validators.js 6.9 KB

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