-
Couldn't load subscription status.
- Fork 0
Plugin Brazil
Marcelo de Souza Lima edited this page Jan 22, 2025
·
2 revisions
The Brazil Plugin extends the functionality of JsBaseClass by adding utility methods specific to Brazil, such as CPF validation. This plugin is designed to simplify common tasks related to Brazilian data formats and regulations.
-
CPF Validation:
- Validate Brazilian CPF (Cadastro de Pessoas Físicas) numbers.
- Checks for invalid patterns and calculates the correct check digits.
Include the plugin script after including JsBaseClass:
<!-- Include JsBaseClass first -->
<script src="path/to/jsBaseClass.min.js"></script>
<!-- Include the Brazil Plugin -->
<script src="path/to/jsBaseClassPluginBrazil.min.js"></script>To validate a CPF, use the isValidCpf method provided by the brazil object:
class ClassBrazilValidator extends JsBaseClass {
async handle() {
const cpf = '111.111.111-11'; // Example CPF
const valid = this.brazil.isValidCpf(cpf);
if (valid) {
this.console.success(`🚀 ${cpf} is valid`);
} else {
this.console.error(`🚨 ${cpf} is invalid!!!`);
}
}
}
// Initialize the validator
window.objBrazilValidator = new ClassBrazilValidator();
objBrazilValidator.init();-
Input Cleaning:
- The
isValidCpfmethod removes non-digit characters (e.g.,.,-) from the input.
- The
-
Length Check:
- Ensures the CPF has exactly 11 digits.
-
Invalid Pattern Check:
- Rejects CPFs with all digits identical (e.g.,
000.000.000-00).
- Rejects CPFs with all digits identical (e.g.,
-
Check Digit Calculation:
- Validates the CPF by calculating and comparing the check digits.
🚨 111.111.111-11 is invalid!!!
- Accurate Validation: Ensures CPF numbers comply with Brazilian regulations.
- Easy Integration: Seamlessly integrates with JsBaseClass.
-
Custom Logging: Leverages the
consoleobject for consistent and readable logs.
- Sanitize Input: Always sanitize user input before validation to avoid errors.
- Use in Forms: Integrate CPF validation into forms to ensure data accuracy.
- Combine with Other Validations: Use alongside other validation methods for comprehensive data checks.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Brazil Plugin Example</title>
<script src="path/to/jsBaseClass.min.js"></script>
<script src="path/to/jsBaseClassPluginBrazil.min.js"></script>
</head>
<body>
<h1>Brazil Plugin Example</h1>
<p>Open the browser console to see the validation results.</p>
<script>
class ClassBrazilValidator extends JsBaseClass {
async handle() {
const cpf = '111.111.111-11'; // Example CPF
const valid = this.brazil.isValidCpf(cpf);
if (valid) {
this.console.success(`🚀 ${cpf} is valid`);
} else {
this.console.error(`🚨 ${cpf} is invalid!!!`);
}
}
}
// Initialize the validator
window.objBrazilValidator = new ClassBrazilValidator();
objBrazilValidator.init();
</script>
</body>
</html>The Brazil Plugin is licensed under the MIT License.
Contributions are welcome! Please open an issue or submit a pull request on the GitHub repository.
Enjoy using the Brazil Plugin with JsBaseClass! 🚀