Created
November 27, 2015 22:04
-
-
Save rafaell-lycan/f7607bcb43851d4b9252 to your computer and use it in GitHub Desktop.
CommonValidatorService.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() { | |
'use strict'; | |
angular | |
.module('auth') | |
.factory('CommonValidatorService', CommonValidatorService); | |
function CommonValidatorService() { | |
function validateEmail(email) { | |
return email.match(/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$/); | |
} | |
function validateDate(date, minAgeLimit) { | |
let stringDateFormated = date.replace(/(\d{2})(\d{2})(\d{4})/, '$1/$2/$3'); | |
if (!stringDateFormated) return; | |
let dateArray = stringDateFormated.split('/'); | |
let day = parseInt(dateArray[0], 10) || 0; | |
let month = parseInt(dateArray[1], 10) || 0; | |
let year = parseInt(dateArray[2], 10) || 0; | |
let currentYear = new Date().getFullYear(); | |
if (day <= 0 || day > 31) return; // Days | |
if (month <= 0 || month > 12) return; // Months | |
if (year <= 0 || year > currentYear) return; // Year | |
if (minAgeLimit && minAgeLimit < (year - currentYear)) return; // Minimum age limit | |
return true; | |
} | |
function validatePassword(password) { | |
if (!password.match(/^(?=.*[a-z])(?=.*[A-Z]).+$/)) return; | |
if (!password.match(/^(?=.*\d).+$/)) return; | |
return password.length >= 8; | |
} | |
function validateCPF(cpf) { | |
var amount = 0; | |
var total; | |
var i; | |
for (var i = 0; i < 10; i++) { | |
if ('' + cpf === '' + i + i + i + i + i + i + i + i + i + i + i) return; | |
} | |
for (i = 1; i <= 9; i++) { | |
amount += Math.floor(cpf.charAt(i - 1)) * (11 - i); | |
} | |
total = 11 - (amount - (Math.floor(amount / 11) * 11)); | |
if ((total == 10) || (total == 11)) total = 0; | |
if (total != Math.floor(cpf.charAt(9))) return; | |
amount = 0; | |
for (i = 1; i <= 10; i++) { | |
amount += cpf.charAt(i - 1) * (12 - i); | |
} | |
total = 11 - (amount - (Math.floor(amount / 11) * 11)); | |
if ((total == 10) || (total == 11)) total = 0; | |
if (total != Math.floor(cpf.charAt(10))) return; | |
return true; | |
} | |
function validateCNPJ(cpnj) { | |
var origin = cpnj.substr(0, 12); | |
var checkDigit = cpnj.substr(12, 2); | |
var amount = 0; | |
var i; | |
for (i = 0; i < 12; i++) { | |
amount += origin.charAt(11 - i) * (2 + (i % 8)); | |
} | |
if (amount == 0) return; | |
amount = 11 - (amount % 11); | |
if (amount > 9) amount = 0; | |
if (checkDigit.charAt(0) != amount) return; | |
amount *= 2; | |
for (i = 0; i < 12; i++) { | |
amount += origin.charAt(11 - i) * (2 + ((i + 1) % 8)); | |
} | |
amount = 11 - (amount % 11); | |
if (amount > 9) amount = 0; | |
if (checkDigit.charAt(1) != amount) return; | |
return true; | |
} | |
return { | |
validateEmail: validateEmail, | |
validatePassword: validatePassword, | |
validateDate: validateDate, | |
validateCPF: validateCPF, | |
validateCNPJ: validateCNPJ, | |
}; | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment