br-phone-validator-lib
Version:
Cell number and fixed phone number validator lib
25 lines (24 loc) • 884 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const tryFormatterPhone = (phone) => {
let newString;
if (phone.length === 11) {
newString = '(' + phone.substr(0, 2) + ')' + ' ' + phone.substr(2, 5) + '-' + phone.substr(7, phone.length);
}
else {
newString = '(' + phone.substr(0, 2) + ')' + ' ' + phone.substr(2, 4) + '-' + phone.substr(6, phone.length);
}
return newString;
};
const PhoneBrValidator = {
isPhone(phone) {
const exp = /\([1-9]{2}\) (?:[2-8]|9[1-9])[0-9]{3}-[0-9]{4}/g;
let result = exp.test(phone);
if (!result && (phone.length === 11 || phone.length === 10)) {
const attemptResult = tryFormatterPhone(phone);
result = exp.test(attemptResult);
}
return result;
}
};
exports.default = PhoneBrValidator;