validations-br
Version:
A validator to BR informations
30 lines (28 loc) • 1.02 kB
JavaScript
const require_validations_utils = require('./utils.cjs');
//#region src/validations/validatePhone.ts
/**
* The function `validatePhone` validates a Brazilian phone number.
* @param {string} phone - The `phone` parameter is a string that represents the phone number to be
* validated.
* @returns The function `validatePhone` returns a boolean value. It returns `true` if the phone number
* is valid, and `false` otherwise.
*/
function validatePhone(phone) {
const clearPhone = phone.replace(/\D/g, "");
if (require_validations_utils.isRepeated(clearPhone)) return false;
if (!(clearPhone.length >= 8 && clearPhone.length <= 11)) return false;
if (clearPhone.length > 9 && [0, 1].indexOf(clearPhone.indexOf("0")) !== -1) return false;
const shortNumber = clearPhone.length > 9 ? clearPhone.substring(2) : clearPhone;
if (shortNumber.length === 8) return [
2,
3,
4,
5,
6,
7,
8
].indexOf(+shortNumber[0]) !== -1;
return shortNumber[0] === "9";
}
//#endregion
exports.validatePhone = validatePhone;