Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Validator {
- constructor(form, constraints, onChange = true) {
- this.form = form;
- this.constraints = constraints;
- this.ON_CHANGE = onChange;
- this.ERROR = 'has-error';
- };
- init() {
- this._validate();
- };
- checkPresence(input) {
- return (input.value.length >= 1) ? true : false;
- };
- checkEmail(input) {
- let email = input.value;
- 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,}))$/;
- return re.test(String(email).toLowerCase());
- };
- handleFormSubmit(e, form) {
- let errors = this.validate(form, this.constraints);
- let keys = Object.keys(errors);
- if (keys.length) {
- for (let i = 0; i < keys.length; i++) {
- let inputs = form.querySelectorAll('input[name="'+keys[i]+'"], textarea[name="'+keys[i]+'"], select[name="'+keys[i]+'"]');
- for (let j = 0; j < inputs.length; j++) {
- if (inputs[j].name === keys[i]) {
- e.preventDefault();
- inputs[j].classList.add(this.ERROR);
- if (this.addError) {
- this.addError(inputs[j]);
- };
- };
- };
- };
- } else {
- if (this.removeAllErrors) {
- this.removeAllErrors(e, form);
- };
- if (this.showSuccess) {
- this.showSuccess(e, form);
- };
- };
- };
- handleChangeInput(input, error) {
- if (error) {
- input.classList.add(this.ERROR);
- if (this.addError) {
- this.addError(input);
- };
- } else {
- input.classList.remove(this.ERROR);
- if (this.removeError) {
- this.removeError(input);
- };
- };
- };
- validate(form, constraints) {
- let inputs = [].slice.call(form.querySelectorAll('input, select, textarea'));
- let errors = {};
- inputs.forEach((input) => {
- let name = input.name;
- let error = this.validateInput(input, constraints[name]);
- if (error) {
- errors[name] = error;
- };
- });
- return errors;
- };
- validateInput(input, constraint) {
- if (constraint === 'presence' && !this.checkPresence(input)) {
- return input.name;
- };
- if (constraint === 'email' && !this.checkEmail(input)) {
- return input.name;
- };
- if (typeof constraint === 'function' && !constraint(input)) {
- return input.name;
- };
- };
- _validate() {
- this.form.addEventListener('submit', (e) => {
- this.handleFormSubmit(e, this.form);
- }, false);
- if (this.ON_CHANGE) {
- let inputs = [].slice.call(this.form.querySelectorAll('input, select, textarea'));
- inputs.forEach((input) => {
- input.addEventListener('change', (e) => {
- let errors = this.validate(this.form, this.constraints);
- this.handleChangeInput(e.currentTarget, errors[e.currentTarget.name]);
- }, false);
- });
- };
- };
- };
- const $forms = $('.js-form-valid');
- function checkLength(input) {
- return (input.value.length > 5) ? true : false;
- };
- function checkCheckbox(input) {
- return input.checked ? true : false;
- };
- $forms.each(function(index, el) {
- let validator = new Validator(el, {
- username: checkLength,
- email: 'email',
- checkbox: checkCheckbox,
- comment: 'presence'
- });
- validator.removeAllErrors = (e, form) => {
- };
- validator.addError = (input) => {
- };
- validator.removeError = (input) => {
- };
- validator.showSuccess = (e, form) => {
- };
- validator.init();
- });
Add Comment
Please, Sign In to add comment