Guest User

Untitled

a guest
Oct 23rd, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.45 KB | None | 0 0
  1. class Validator {
  2. constructor(form, constraints, onChange = true) {
  3. this.form = form;
  4. this.constraints = constraints;
  5. this.ON_CHANGE = onChange;
  6. this.ERROR = 'has-error';
  7. };
  8.  
  9. init() {
  10. this._validate();
  11. };
  12.  
  13. checkPresence(input) {
  14. return (input.value.length >= 1) ? true : false;
  15. };
  16.  
  17. checkEmail(input) {
  18. let email = input.value;
  19. let re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
  20. return re.test(String(email).toLowerCase());
  21. };
  22.  
  23. handleFormSubmit(e, form) {
  24. let errors = this.validate(form, this.constraints);
  25. let keys = Object.keys(errors);
  26.  
  27. if (keys.length) {
  28. for (let i = 0; i < keys.length; i++) {
  29. let inputs = form.querySelectorAll('input[name="'+keys[i]+'"], textarea[name="'+keys[i]+'"], select[name="'+keys[i]+'"]');
  30. for (let j = 0; j < inputs.length; j++) {
  31. if (inputs[j].name === keys[i]) {
  32. e.preventDefault();
  33. inputs[j].classList.add(this.ERROR);
  34. if (this.addError) {
  35. this.addError(inputs[j]);
  36. };
  37. };
  38. };
  39. };
  40. } else {
  41. if (this.removeAllErrors) {
  42. this.removeAllErrors(e, form);
  43. };
  44. if (this.showSuccess) {
  45. this.showSuccess(e, form);
  46. };
  47. };
  48.  
  49. };
  50.  
  51. handleChangeInput(input, error) {
  52. if (error) {
  53. input.classList.add(this.ERROR);
  54. if (this.addError) {
  55. this.addError(input);
  56. };
  57. } else {
  58. input.classList.remove(this.ERROR);
  59. if (this.removeError) {
  60. this.removeError(input);
  61. };
  62. };
  63. };
  64.  
  65. validate(form, constraints) {
  66. let inputs = [].slice.call(form.querySelectorAll('input, select, textarea'));
  67. let errors = {};
  68. inputs.forEach((input) => {
  69. let name = input.name;
  70. let error = this.validateInput(input, constraints[name]);
  71. if (error) {
  72. errors[name] = error;
  73. };
  74. });
  75. return errors;
  76. };
  77.  
  78. validateInput(input, constraint) {
  79. if (constraint === 'presence' && !this.checkPresence(input)) {
  80. return input.name;
  81. };
  82. if (constraint === 'email' && !this.checkEmail(input)) {
  83. return input.name;
  84. };
  85. if (typeof constraint === 'function' && !constraint(input)) {
  86. return input.name;
  87. };
  88. };
  89.  
  90. _validate() {
  91. this.form.addEventListener('submit', (e) => {
  92. this.handleFormSubmit(e, this.form);
  93. }, false);
  94.  
  95. if (this.ON_CHANGE) {
  96. let inputs = [].slice.call(this.form.querySelectorAll('input, select, textarea'));
  97. inputs.forEach((input) => {
  98. input.addEventListener('change', (e) => {
  99. let errors = this.validate(this.form, this.constraints);
  100. this.handleChangeInput(e.currentTarget, errors[e.currentTarget.name]);
  101. }, false);
  102. });
  103. };
  104. };
  105. };
  106.  
  107. const $forms = $('.js-form-valid');
  108.  
  109. function checkLength(input) {
  110. return (input.value.length > 5) ? true : false;
  111. };
  112.  
  113. function checkCheckbox(input) {
  114. return input.checked ? true : false;
  115. };
  116.  
  117.  
  118.  
  119. $forms.each(function(index, el) {
  120. let validator = new Validator(el, {
  121. username: checkLength,
  122. email: 'email',
  123. checkbox: checkCheckbox,
  124. comment: 'presence'
  125. });
  126.  
  127. validator.removeAllErrors = (e, form) => {
  128.  
  129. };
  130. validator.addError = (input) => {
  131.  
  132. };
  133. validator.removeError = (input) => {
  134.  
  135. };
  136. validator.showSuccess = (e, form) => {
  137.  
  138. };
  139. validator.init();
  140. });
Add Comment
Please, Sign In to add comment