validators.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. fb.warn($(elm).prev('label').text());
  17. return ['<strong>', $(elm).prev('label').text(), '</strong>'].join('');
  18. }
  19. App.Validate.Rule = {
  20. 'required' : function(elm) {
  21. if ($(elm).val().trim() == '') {
  22. return {VALID: false, ERROR: App.Validate.getFieldName(elm) + ' is required'};
  23. }
  24. return {VALID: true};
  25. },
  26. 'no-spaces': function(elm) {
  27. if ($(elm).val().search(/\s/) != -1) {
  28. return {VALID: false, ERROR: App.Validate.getFieldName(elm) + ' cannot contain spaces'};
  29. }
  30. return {VALID: true};
  31. },
  32. 'abc': function(elm) {
  33. if ($(elm).val().search(/[^a-zA-Z]+/) != -1) {
  34. return {VALID: false, ERROR: App.Validate.getFieldName(elm) + ' must contain only letters'};
  35. }
  36. return {VALID: true};
  37. },
  38. 'email': function(elm) {
  39. if ($(elm).val().search(/^\s*[\w\-\+_]+(\.[\w\-\+_]+)*\@[\w\-\+_]+\.[\w\-\+_]+(\.[\w\-\+_]+)*\s*$/) == -1) {
  40. return {VALID: false, ERROR: App.Validate.getFieldName(elm) + ' not a valid email'};
  41. }
  42. return {VALID: true};
  43. }
  44. }
  45. App.Validate.form = function(world, elm)
  46. {
  47. var form_valid = true;
  48. App.Env.FormError = [];
  49. $(elm).find('select, input, textarea').each(function(i, field)
  50. {
  51. if ($.inArray($(field).attr('name'), ['target', 'source', 'save']) != -1) {
  52. //return; // pass
  53. }
  54. else {
  55. var rules = App.Validate.getRules(field);
  56. $(rules).each(function(i, rule)
  57. {
  58. fb.log('Validate with %o %o', rule, field);
  59. if (App.Validate.Rule[rule]) {
  60. var result = App.Validate.Rule[rule](field);
  61. fb.log(result);
  62. if (result.VALID == false) {
  63. App.Env.FormError.push(result.ERROR); //$(field).attr('name') + ' is required');
  64. form_valid = false;
  65. }
  66. }
  67. })
  68. /*if ($(field).val().trim() == '' || $(field).val().trim() == '-') {
  69. App.Env.FormError.push($(field).attr('name') + ' is required');
  70. form_valid = false;
  71. }*/
  72. }
  73. });
  74. return form_valid;
  75. }
  76. App.Validate.displayFormErrors = function(world, elm)
  77. {
  78. var errors_tpl = '';
  79. $(App.Env.FormError).each(function(i, error)
  80. {
  81. var tpl = App.Templates.get('error_elm', 'general');
  82. tpl.set(':ERROR', error);
  83. errors_tpl += tpl.finalize();
  84. });
  85. var ref = $('.form-error', elm);
  86. ref.removeClass('hidden');
  87. ref.html(errors_tpl);
  88. }
  89. App.Validate.getRules = function(elm)
  90. {
  91. var rules_string = $(elm).attr('class');
  92. var rules = [];
  93. $(rules_string.split(/\s/)).each(function(i, str)
  94. {
  95. var rule = str.split('rule-');
  96. if (rule.length > 1) {
  97. rules[rules.length++] = rule[1];
  98. }
  99. });
  100. return rules;
  101. }