//angular //libs //this export class ValidatorFields { constructor() { } public regex = { email: '[A-Za-z0-9.!#$%&’*+/=?^_`{|}~-]+@[A-Za-z0-9-]+(?:\.[A-Za-z0-9-]+)*', wordsOnly: '[A-Za-z]+', wordsUnderline: '[A-Za-z_]+', words: '[A-Za-zéúíóáÉÚÍÓÁèùìòàçÇÈÙÌÒÀõãñÕÃÑêûîôâÊÛÎÔÂëÿüïöäËYÜÏÖÄ]+', wordsSpace: '((?:[ ])?([A-Za-zéúíóáÉÚÍÓÁèùìòàçÇÈÙÌÒÀõãñÕÃÑêûîôâÊÛÎÔÂëÿüïöäËYÜÏÖÄ]+))+', wordsSpaceDot: "((?:[ ])?([A-Za-zéúíóáÉÚÍÓÁèùìòàçÇÈÙÌÒÀõãñÕÃÑêûîôâÊÛÎÔÂëÿüïöäËYÜÏÖÄ]+)(.{1}))+", wordsSpaceNumberDot: "((?:[ ])?([A-Za-zéúíóáÉÚÍÓÁèùìòàçÇÈÙÌÒÀõãñÕÃÑêûîôâÊÛÎÔÂëÿüïöäËYÜÏÖÄ0-9]+)(.{1}))+", wordsSpaceNumber: '[A-Za-zéúíóáÉÚÍÓÁèùìòàçÇÈÙÌÒÀõãñÕÃÑêûîôâÊÛÎÔÂëÿüïöäËYÜÏÖÄ 0-9]+', wordsSpaceEnterNumber: '[A-Za-zéúíóáÉÚÍÓÁèùìòàçÇÈÙÌÒÀõãñÕÃÑêûîôâÊÛÎÔÂëÿüïöäËYÜÏÖÄ 0-9 \n]+', onlyWordsNumber: '[A-Za-z0-9]+', cpf: '[0-9]{3}\.?[0-9]{3}\.?[0-9]{3}\-?[0-9]{2}', rg: '[0-9]{1,3}\.?[0-9]{2,3}\.?[0-9]{3}\-?[0-9]{1}[\s _]*', cell: '[\(][0-9]{2}[\)] [0-9]{5}[-][0-9]{4}', telephone: '[\(][0-9]{2}[\)] [0-9]{4}[-][0-9]{4}', // date: '[0-9]{2}[-|\/]{1}[0-9]{2}[-|\/]{1}[0-9]{4}' date: '[0-9]{2}[/]{1}[0-9]{2}[/]{1}[0-9]{4}', date2: '([0-9]){4}[-]([0-9]){2}[-]([0-9]){2}', time: '(?:0[0123456789]|1[0123456789]|2[0123])([h]*[:])(?:[012345][0123456789])(?:([:]*)[012345][0123456789])*', idProfileFacebook: '((.*)(referrer_profile_id=)([0-9]+)|[0-9]+)'//'((.)*referrer_profile_id=([0-9]{15}))|((.)*referrer_profile_id=([0-9]{10}))|([0-9]{15})|([0-9]{10})' }; // http://turing.com.br/material/regex/introducao.html public getMsg(value: string, params?: any[]) { let message = this.message[value]; if (params) { params.forEach(element => { for (let i = 0; i < params.length; i++) { message = message.replace('{' + i + '}', params[i]); } }); } return message; } public message = { fieldObrigatorio: 'Ops! Este campo é obrigatório.', equalValue: 'Ops! Este valor não corresponde.', wordsOnly: 'Ops! Este campo aceita somente letras.', words: 'Ops! Este campo aceita somente uma palavra.', wordsSpace: 'Ops! Este campo aceita somente letras.', wordsSpaceDot: 'Ops! Este campo aceita somente letras, ponto e vírgula.', wordsSpaceNumber: 'Ops! Este campo aceita somente letras, números e espaços.', wordsSpaceNumberDot: 'Ops! Este campo aceita somente letras, números, espaços, ponto e vírgula.', wordsSpaceEnterNumber: 'Ops! Este campo aceita somente letras, números, quebra de linha e espaços.', email: 'Ops! Este email não é válido.', cpf: 'Ops! Este cpf não é válido.', rg: 'Ops! Este rg não é válido.', cell: 'Ops! Este celular não é válido.', telephone: 'Ops! Este telefone não é válido.', date: 'Ops! A Data não é válida.', messageDefaultPattern: 'Ops! A Informação não é válida.', time: 'Ops! A Hora não é válida.', incorrectValue: 'Ops! Este valor não é válido.', minLength: 'Ops! Mínimo {0} caracteres.', maxLength: 'Ops! Máximo {0} caracteres.', idProfileFacebook: 'Ops! Este link não é válido.' } // public validateForm(formGroup: FormGroup) { // // formGroup.updateValueAndValidity() // } /* public validate(formulario: NgForm, field, message?) { message = message ? message : 'default'; return (formulario.form.controls[field] && formulario.form.controls[field].touched && formulario.form.controls[field].errors) && (formulario.form.controls[field].errors.required || formulario.form.controls[field].errors.pattern) ? this.getMessage(formulario.form.controls[field].errors, message) : true; } private getMessage(errors, message) { if (errors.required) { return this.message['fieldObrigatorio']; } else if (errors.pattern) { return this.message.pattern[message]; } } */ public resolveDate(value): Date { let regex = /(\d+)[\/-](\d+)[\/-](\d+)/gi; value.replace(regex, '$1, $2, $3'); if (RegExp.$3.length == 4 && RegExp.$2.length == 2 && RegExp.$1.length == 2) { return new Date(+RegExp.$3, +RegExp.$2 - 1, +RegExp.$1); } else if (RegExp.$1.length == 4 && RegExp.$2.length == 2 && RegExp.$3.length == 2) { return new Date(+RegExp.$1, +RegExp.$2 - 1, +RegExp.$3); } else { return undefined; } } public resolveDateTime(value): Date { if (value) { let regex = /(\d+)[\/-](\d+)[\/-](\d+)[ ](\d+)[h][:](\d+)/; value.replace(regex, '$1, $2, $3', '$4', '$5'); if (RegExp.$3.length == 4 && RegExp.$2.length == 2 && RegExp.$1.length == 2 && RegExp.$4.length == 2 && RegExp.$5.length == 2) { return new Date(+RegExp.$3, +RegExp.$2 - 1, +RegExp.$1, +RegExp.$4, +RegExp.$5); } else if (RegExp.$1.length == 4 && RegExp.$2.length == 2 && RegExp.$3.length == 2 && RegExp.$4.length == 2 && RegExp.$5.length == 2) { return new Date(+RegExp.$1, +RegExp.$2 - 1, +RegExp.$3, +RegExp.$4, +RegExp.$5); } else { return undefined; } } else { return undefined; } } public validateDate(date_inicio, date_fim): Date[] | boolean { let dateValid1 = this.validateOneDate(date_inicio); let dateValid2 = this.validateOneDate(date_fim); return dateValid1 != undefined && dateValid2 != undefined ? [dateValid1, dateValid2] : false; } private validateOneDate(value): Date { let regex = /(\d{2})[\/](\d{2})[\/](\d{4})[ ](\d{2})[h][:](\d{2})/gi; let strInicio = value + ''; let day = strInicio.replace(regex, '$1'); let month = strInicio.replace(regex, '$2'); let year = strInicio.replace(regex, '$3'); let hours = strInicio.replace(regex, '$4'); let minutes = strInicio.replace(regex, '$5'); if (day.length == 2 && month.length == 2 && year.length == 4 && hours.length == 2 && minutes.length == 2) { return new Date(+year, +month, +day, +hours, +minutes); } return undefined; } // public validateRG(str, nameField: string, form_control: NgForm): boolean { // return this.validateInput(str, 7, nameField, form_control); // } public validateCPF(str, formControl: any) { if (str) { let regex = /[0-9]{3}\.?[0-9]{3}\.?[0-9]{3}\-?[0-9]{2}/gi; if (!regex.test(str)) { const field = formControl.form.get('cpf'); field .valueChanges .subscribe(val => { field.setErrors({ incorrect: true }); }); } } } public validateInput(str, strLength: number, nameField: string, formControl: any): boolean { if (str) { let regex = /[.\-_\s]*/gi; let newstr: string = str.replace(regex, ''); if (newstr.length == strLength) { if (formControl) { const field = formControl.form.get(nameField); field .valueChanges .subscribe(val => { field.setErrors(undefined); }); return true; } } else if (newstr.length < strLength) { if (formControl) { let field = formControl.form.get(nameField); this.setErrorForm(field, { incorrect: true }); return false; } } } } public validateInputs(str, strLength: number, nameField: string, formControl: any): boolean { if (str) { let regex = /[_.-\s]/gi; let newstr: string = str.replace(regex, ''); if (newstr.length > strLength) { if (formControl) { let field = formControl.form.get(nameField); this.setErrorForm(field, undefined); return true; } } else { if (formControl) { let field = formControl.form.get(nameField); this.setErrorForm(field, { incorrect: true }); return false; } } } } private setErrorForm(field, value) { if (field) { field .valueChanges .subscribe(val => { field.setErrors(value); }); } } public dateTimeToString(valueDate: Date): string { let day: String = valueDate.getDate() < 10 ? '0' + valueDate.getDate() : valueDate.getDate().toString(); let month: String = (valueDate.getMonth() + 1) < 10 ? '0' + (valueDate.getMonth() + 1) : (valueDate.getMonth() + 1).toString(); let year: String = valueDate.getFullYear().toString(); let hours: String = valueDate.getHours() < 10 ? '0' + valueDate.getHours() : valueDate.getHours().toString(); let minutes: String = valueDate.getMinutes() < 10 ? '0' + valueDate.getMinutes() : valueDate.getMinutes().toString(); return day + '/' + month + '/' + year + ' ' + hours + 'h:' + minutes; } public removeSpaceString(str): string { if (str) { let regex = /\s/gi; let newStr: string = str.replace(regex, ''); return newStr; } else { return undefined; } } }