A PHP library for validating Ecuadorian identification documents:
- Cedula (National ID - 10 digits)
- RUC for Natural Persons (13 digits, third digit 0-5)
- RUC for Private Companies (13 digits, third digit 9)
- RUC for Public Companies (13 digits, third digit 6)
- PHP 8.2 or higher
composer require tavo1987/ec-validador-cedula-rucrequire 'vendor/autoload.php';
use Tavo\ValidadorEc;
$validator = new ValidadorEc();For simple validation without creating an instance:
// Validate any document type
if (ValidadorEc::isValid('0926687856')) {
echo 'Valid document';
}
// Validate specific document types
ValidadorEc::isValidCedula('0926687856'); // true
ValidadorEc::isValidNaturalPersonRuc('0926687856001'); // true
ValidadorEc::isValidPrivateCompanyRuc('0992397535001'); // true
ValidadorEc::isValidPublicCompanyRuc('1760001550001'); // trueThe validate() method automatically detects and validates any document type:
// Cedula (10 digits)
if ($validator->validate('0926687856')) {
echo 'Valid: ' . $validator->getDocumentType(); // "cedula"
}
// Natural Person RUC (13 digits, third digit 0-5)
if ($validator->validate('0926687856001')) {
echo 'Valid: ' . $validator->getDocumentType(); // "ruc_natural"
}
// Public Company RUC (13 digits, third digit 6)
if ($validator->validate('1760001550001')) {
echo 'Valid: ' . $validator->getDocumentType(); // "ruc_public"
}
// Private Company RUC (13 digits, third digit 9)
if ($validator->validate('0992397535001')) {
echo 'Valid: ' . $validator->getDocumentType(); // "ruc_private"
}If you know the document type beforehand:
// Validate Cedula
if ($validator->validateCedula('0926687856')) {
echo 'Valid Cedula';
} else {
echo 'Error: ' . $validator->getError();
}
// Validate Natural Person RUC
if ($validator->validateNaturalPersonRuc('0926687856001')) {
echo 'Valid RUC';
}
// Validate Private Company RUC
if ($validator->validatePrivateCompanyRuc('0992397535001')) {
echo 'Valid RUC';
}
// Validate Public Company RUC
if ($validator->validatePublicCompanyRuc('1760001550001')) {
echo 'Valid RUC';
}For Natural Person RUC, you can extract the cedula portion:
$cedula = $validator->extractCedulaFromRuc('0926687856001');
// Returns: '0926687856'
// Returns null for Private/Public Company RUC (no cedula)
$result = $validator->extractCedulaFromRuc('0992397535001');
// Returns: null
// Returns null for invalid RUC
$result = $validator->extractCedulaFromRuc('invalid');
// Returns: nullValidadorEc::TYPE_CEDULA // 'cedula'
ValidadorEc::TYPE_RUC_NATURAL // 'ruc_natural'
ValidadorEc::TYPE_RUC_PRIVATE // 'ruc_private'
ValidadorEc::TYPE_RUC_PUBLIC // 'ruc_public'| Method | Description |
|---|---|
ValidadorEc::isValid(string $number) |
Quick validation of any document type |
ValidadorEc::isValidCedula(string $number) |
Quick cedula validation |
ValidadorEc::isValidNaturalPersonRuc(string $number) |
Quick natural person RUC validation |
ValidadorEc::isValidPrivateCompanyRuc(string $number) |
Quick private company RUC validation |
ValidadorEc::isValidPublicCompanyRuc(string $number) |
Quick public company RUC validation |
| Method | Description |
|---|---|
validate(string $number) |
Auto-detect and validate any document |
validateCedula(string $number) |
Validate Cedula (10 digits) |
validateNaturalPersonRuc(string $number) |
Validate Natural Person RUC (13 digits) |
validatePrivateCompanyRuc(string $number) |
Validate Private Company RUC (13 digits) |
validatePublicCompanyRuc(string $number) |
Validate Public Company RUC (13 digits) |
extractCedulaFromRuc(string $ruc) |
Extract cedula from natural person RUC |
getDocumentType() |
Get detected document type after validation |
getError() |
Get error message from last validation |
| Document Type | Digits | Third Digit | Algorithm |
|---|---|---|---|
| Cedula | 10 | 0-5* | Modulo 10 |
| RUC Natural Person | 13 | 0-5* | Modulo 10 |
| RUC Private Company | 13 | 9 | Modulo 11 |
| RUC Public Company | 13 | 6 | Modulo 11 |
*For province code 30, third digit validation is skipped (see below).
The first two digits represent the province:
| Code | Province |
|---|---|
| 01 | Azuay |
| 02 | Bolivar |
| 03 | Cañar |
| 04 | Carchi |
| 05 | Cotopaxi |
| 06 | Chimborazo |
| 07 | El Oro |
| 08 | Esmeraldas |
| 09 | Guayas |
| 10 | Imbabura |
| 11 | Loja |
| 12 | Los Rios |
| 13 | Manabi |
| 14 | Morona Santiago |
| 15 | Napo |
| 16 | Pastaza |
| 17 | Pichincha |
| 18 | Tungurahua |
| 19 | Zamora Chinchipe |
| 20 | Galapagos |
| 21 | Sucumbios |
| 22 | Orellana |
| 23 | Santo Domingo de los Tsachilas |
| 24 | Santa Elena |
| 30 | Foreign residents |
Province code 30 is for "ecuatorianos registrados en el exterior" (Ecuadorians abroad) and foreign residents:
- Third digit validation is skipped
- Check digit (Modulo 10) validation is still performed
This library uses algorithmic validation (Modulo 10/11). Known limitations:
-
Foreign natural persons without cedula: RUCs for foreigners may not follow standard validation. Verify through SRI web services.
-
Critical applications: Consider complementing with official SRI database queries.
# Run all tests
./vendor/bin/phpunit
# Run with coverage
./vendor/bin/phpunit --coverage-text- Breaking: Minimum PHP version is now 8.2
- Breaking: All method names now in English
- Breaking: Constants renamed (
TIPO_*→TYPE_*) - Breaking: Removed all Spanish method names
- New universal
validate()method with auto-detection - New
getDocumentType()method - New static methods for quick validation (
isValid(),isValidCedula(), etc.) - New
extractCedulaFromRuc()method to extract cedula from natural person RUC - Support for province code 30 (foreign residents)
- Class is now
final - Optimized with
matchexpressions - 93 comprehensive tests
- Fixed namespace issues
- Initial release
MIT License - see LICENSE file for details.
Edwin Ramirez
- Twitter: @edwin_tavo
- GitHub: @tavo1987
Bryan Suarez
- Twitter: @BryanSC_7
