validators.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. 'minute': function(elm) {
  78. if ($(elm).val() == '*') {
  79. return {VALID: true};
  80. }
  81. var minute = parseInt($(elm).val(), 10);
  82. if (minute > 60 || minute < 0) {
  83. return {VALID: false, ERROR: App.Validate.getFieldName(elm) + ' wrong minute value'};
  84. }
  85. return {VALID: true};
  86. },
  87. 'hour': function(elm) {
  88. if ($(elm).val() == '*') {
  89. return {VALID: true};
  90. }
  91. var hour = parseInt($(elm).val(), 10);
  92. if (hour > 60 || hour < 0) {
  93. return {VALID: false, ERROR: App.Validate.getFieldName(elm) + ' wrong hour value'};
  94. }
  95. return {VALID: true};
  96. },
  97. 'wday': function(elm) {
  98. if ($(elm).val() == '*') {
  99. return {VALID: true};
  100. }
  101. var wday = parseInt($(elm).val(), 10);
  102. if (wday > 7 || wday < 1) {
  103. return {VALID: false, ERROR: App.Validate.getFieldName(elm) + ' wrong week day value'};
  104. }
  105. return {VALID: true};
  106. },
  107. 'month': function(elm) {
  108. if ($(elm).val() == '*') {
  109. return {VALID: true};
  110. }
  111. var month = parseInt($(elm).val(), 10);
  112. if (month > 1 || month < 12) {
  113. return {VALID: false, ERROR: App.Validate.getFieldName(elm) + ' wrong month value'};
  114. }
  115. return {VALID: true};
  116. },
  117. 'day': function(elm) {
  118. if ($(elm).val() == '*') {
  119. return {VALID: true};
  120. }
  121. var day = parseInt($(elm).val(), 10);
  122. if (day > 31 || day < 1) {
  123. return {VALID: false, ERROR: App.Validate.getFieldName(elm) + ' wrong day value'};
  124. }
  125. return {VALID: true};
  126. }
  127. }
  128. App.Validate.form = function(world, elm)
  129. {
  130. var form_valid = true;
  131. App.Env.FormError = [];
  132. $(elm).find('select, input, textarea').each(function(i, field)
  133. {
  134. if ($(field).attr('type') == 'checkbox') {
  135. var value = $(field).attr('checked') ? 'on' : 'off';
  136. $(field).val(value);
  137. }
  138. if ($.inArray($(field).attr('name'), ['target', 'source', 'save']) != -1) {
  139. //return; // pass
  140. }
  141. else {
  142. var rules = App.Validate.getRules(field);
  143. $(rules).each(function(i, rule)
  144. {
  145. fb.log('Validate with %o %o', rule, field);
  146. if (App.Validate.Rule[rule]) {
  147. var result = App.Validate.Rule[rule](field);
  148. fb.log(result);
  149. if (result.VALID == false) {
  150. App.Env.FormError.push(result.ERROR); //$(field).attr('name') + ' is required');
  151. form_valid = false;
  152. }
  153. }
  154. })
  155. /*if ($(field).val().trim() == '' || $(field).val().trim() == '-') {
  156. App.Env.FormError.push($(field).attr('name') + ' is required');
  157. form_valid = false;
  158. }*/
  159. }
  160. });
  161. return form_valid;
  162. }
  163. App.Validate.displayFormErrors = function(world, elm)
  164. {
  165. var errors_tpl = '';
  166. $(App.Env.FormError).each(function(i, error)
  167. {
  168. var tpl = App.Templates.get('error_elm', 'general');
  169. tpl.set(':ERROR', error);
  170. errors_tpl += tpl.finalize();
  171. });
  172. var ref = $('.form-error', elm);
  173. ref.removeClass('hidden');
  174. ref.html(errors_tpl);
  175. }
  176. App.Validate.getRules = function(elm)
  177. {
  178. try {
  179. var rules_string = $(elm).attr('class');
  180. var rules = [];
  181. $(rules_string.split(/\s/)).each(function(i, str)
  182. {
  183. var rule = str.split('rule-');
  184. if (rule.length > 1) {
  185. rules[rules.length++] = rule[1];
  186. }
  187. });
  188. return rules;
  189. }
  190. catch(e) {
  191. return [];
  192. }
  193. }