Last active
July 6, 2022 21:02
-
-
Save marcos-abs/b228caa72c66f5912ac75033c146cd7e to your computer and use it in GitHub Desktop.
Funções - Functions
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
/** | |
* ***** | |
* File: isValidCPF.js | |
* Project: Curso de PHP com PagSeguro da Hcode | |
* Path: ~desenvolvedor/curso-de-php-com-pagseguro-hcode | |
* File Created: Wednesday, 06 July 2022 12:59:56 | |
* Author: Marcos Antônio Barbosa de Souza (marcantech@uol.com.br) | |
* ----- | |
* Last Modified: Wednesday, 06 July 2022 12:59:56 | |
* Modified By: Marcos Antônio Barbosa de Souza (<marcantech@uol.com.br>) | |
* ----- | |
* Copyright (c) 2022 All rights reserved, Marcant Tecnologia da Informação | |
* ----- | |
* Description: | |
* ············ Função Valida CPF dp João Rangel da Hcode(curso de PHP com PagSeguro) | |
* ***** | |
*/ | |
function isValidCPF(cpf) { | |
if (typeof cpf !== 'string') return false; | |
cpf = cpf.replace(/[^\d]+/g, ''); | |
if (cpf.length !== 11 || !!cpf.match(/(\d)\1{10}/)) return false; | |
cpf = cpf.split('').map((el) => +el); | |
const rest = (count) => ((cpf.slice(0, count - 12).reduce((soma, el, index) => soma + el * (count - index), 0) * 10) % 11) % 10; | |
return rest(10) === cpf[9] && rest(11) === cpf[10]; | |
} |
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
<?php | |
# ***** | |
# File: isValidCPF.php | |
# Project: Curso de PHP com PagSeguro da Hcode | |
# Path: ~desenvolvedor/curso-de-php-com-pagseguro-hcode | |
# File Created: Wednesday, 06 July 2022 13:01:13 | |
# Author: Marcos Antônio Barbosa de Souza (marcantech@uol.com.br) | |
# ----- | |
# Last Modified: Wednesday, 06 July 2022 18:02:13 | |
# Modified By: Marcos Antônio Barbosa de Souza (<marcantech@uol.com.br>) | |
# ----- | |
# Copyright (c) 2022 All rights reserved, Marcant Tecnologia da Informação | |
# ----- | |
# Description: | |
# ············ Função Valida CPF em PHP João Rangel da Hcode (curso de PHP com PagSeguro) | |
# ***** | |
function isValidCPF(string $number):bool { | |
$number = preg_replace('/[^0-9]/', '', (string) $number); | |
if (strlen($number) != 11) | |
return false; | |
for ($i = 0, $j = 10, $sum = 0; $i < 9; $i++, $j--) | |
$sum += $number[$i] * $j; | |
$rest = $sum % 11; | |
if ($number[9] != ($rest < 2 ? 0 : 11 - $rest)) | |
return false; | |
for ($i = 0, $j = 11, $sum = 0; $i < 10; $i++, $j--) | |
$sum += $number[$i] * $j; | |
$rest = $sum % 11; | |
return ($number[10] == ($rest < 2 ? 0 : 11 - $rest)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment