-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
267b691
commit 7b86de4
Showing
4 changed files
with
115 additions
and
49 deletions.
There are no files selected for viewing
This file contains 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
44 changes: 44 additions & 0 deletions
44
packages/java/src/main/java/io/github/gabriel_logan/validators/CnpjIsValid/main.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
public class CnpjValidator { | ||
public static boolean isValid(String cnpj) { | ||
// Remova caracteres especiais do CNPJ | ||
cnpj = cnpj.replaceAll("[^0-9]", ""); | ||
|
||
// Verifique se o CNPJ possui 14 dígitos | ||
if (cnpj.length() != 14) { | ||
return false; | ||
} | ||
|
||
// Verifique se todos os dígitos são iguais | ||
if (cnpj.matches("(\\d)\\1{13}")) { | ||
return false; | ||
} | ||
|
||
// Verifique o primeiro dígito verificador | ||
int sum = 0; | ||
int weight = 2; | ||
for (int i = 11; i >= 0; i--) { | ||
int digit = Character.getNumericValue(cnpj.charAt(i)); | ||
sum += digit * weight; | ||
weight = (weight + 1) % 9 + 2; | ||
} | ||
int digit1 = (sum % 11 < 2) ? 0 : 11 - (sum % 11); | ||
if (Character.getNumericValue(cnpj.charAt(12)) != digit1) { | ||
return false; | ||
} | ||
|
||
// Verifique o segundo dígito verificador | ||
sum = 0; | ||
weight = 2; | ||
for (int i = 12; i >= 0; i--) { | ||
int digit = Character.getNumericValue(cnpj.charAt(i)); | ||
sum += digit * weight; | ||
weight = (weight + 1) % 9 + 2; | ||
} | ||
int digit2 = (sum % 11 < 2) ? 0 : 11 - (sum % 11); | ||
if (Character.getNumericValue(cnpj.charAt(13)) != digit2) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
packages/java/src/main/java/io/github/gabriel_logan/validators/CpfIsValid/main.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// CpfValidator.java | ||
package io.github.gabriel_logan; | ||
|
||
import io.github.gabriel_logan.types.ValidationResult; | ||
|
||
public class CpfValidator { | ||
private String getErrorMessage(int code) { | ||
// Implemente esta função para retornar a mensagem de erro apropriada | ||
return ""; | ||
} | ||
public ValidationResult cpfIsValid(String cpf) { | ||
try { | ||
int numeroBase = 10; | ||
int numeroBase2 = 11; | ||
int somaTotal = 0; | ||
int somaTotal2 = 0; | ||
|
||
if (cpf.length() != 11) { | ||
return new ValidationResult(false, getErrorMessage(2)); | ||
} | ||
|
||
int primeiroVerificador = 0; | ||
int segundoVerificador = 0; | ||
|
||
for (int repetidor = 0; repetidor < 11; repetidor++) { | ||
int multiplicador = Character.getNumericValue(cpf.charAt(repetidor)) * numeroBase; | ||
numeroBase--; | ||
somaTotal += multiplicador; | ||
|
||
int multiplicador2 = Character.getNumericValue(cpf.charAt(repetidor)) * numeroBase2; | ||
numeroBase2--; | ||
somaTotal2 += multiplicador2; | ||
|
||
int valorDeVerificacao = somaTotal - Character.getNumericValue(cpf.charAt(9)); | ||
int valorDeVerificacao2 = somaTotal2 - Character.getNumericValue(cpf.charAt(10)); | ||
|
||
primeiroVerificador = 11 - (valorDeVerificacao % 11); | ||
segundoVerificador = 11 - (valorDeVerificacao2 % 11); | ||
} | ||
|
||
if (primeiroVerificador > 9) primeiroVerificador = 0; | ||
if (segundoVerificador > 9) segundoVerificador = 0; | ||
|
||
if (primeiroVerificador == Character.getNumericValue(cpf.charAt(9)) | ||
&& segundoVerificador == Character.getNumericValue(cpf.charAt(10))) { | ||
return new ValidationResult(true, null); | ||
} | ||
|
||
return new ValidationResult(false, getErrorMessage(2)); | ||
} catch (Exception e) { | ||
return new ValidationResult(false, getErrorMessage(3)); | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
packages/java/src/main/java/io/github/gabriel_logan/validators/isEmail/main.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import java.util.regex.Pattern; | ||
|
||
public class Main { | ||
public static void main(String[] args) { | ||
String email = "example@example.com"; | ||
boolean isValidEmail = isEmail(email); | ||
System.out.println("Is email valid? " + isValidEmail); | ||
} | ||
|
||
public static boolean isEmail(String email) { | ||
String emailRegex = "^[A-Za-z0-9+_.-]+@[A-Za-z0-9.-]+$"; | ||
Pattern pattern = Pattern.compile(emailRegex); | ||
return pattern.matcher(email).matches(); | ||
} | ||
} |