Skip to content

Plugin Brazil

Marcelo de Souza Lima edited this page Jan 22, 2025 · 2 revisions

Introduction

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.


Features

  1. CPF Validation:
    • Validate Brazilian CPF (Cadastro de Pessoas Físicas) numbers.
    • Checks for invalid patterns and calculates the correct check digits.

Installation

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>

Usage

CPF Validation

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();

How It Works

  1. Input Cleaning:

    • The isValidCpf method removes non-digit characters (e.g., ., -) from the input.
  2. Length Check:

    • Ensures the CPF has exactly 11 digits.
  3. Invalid Pattern Check:

    • Rejects CPFs with all digits identical (e.g., 000.000.000-00).
  4. Check Digit Calculation:

    • Validates the CPF by calculating and comparing the check digits.

Example Output

🚨 111.111.111-11 is invalid!!!

Why Use This Plugin?

  • Accurate Validation: Ensures CPF numbers comply with Brazilian regulations.
  • Easy Integration: Seamlessly integrates with JsBaseClass.
  • Custom Logging: Leverages the console object for consistent and readable logs.

Best Practices

  • 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.

Example: Full Integration

<!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>

License

The Brazil Plugin is licensed under the MIT License.


Contributing

Contributions are welcome! Please open an issue or submit a pull request on the GitHub repository.


Enjoy using the Brazil Plugin with JsBaseClass! 🚀

Clone this wiki locally