nigeria-validator
Version:
Validate and format common Nigerian data: phone numbers, BVNs, bank codes, etc.
21 lines (17 loc) • 553 B
JavaScript
// src/phone.js
const isValidPhone = (phone) => {
const cleaned = phone.replace(/\D/g, "");
return (
/^0[789][01]\d{8}$/.test(cleaned) || /^234[789][01]\d{8}$/.test(cleaned)
);
};
const formatPhone = (phone, format = "local") => {
let cleaned = phone.replace(/\D/g, "");
if (cleaned.startsWith("234")) {
cleaned = "0" + cleaned.slice(3);
}
if (format === "local") return cleaned;
if (format === "intl") return "+234" + cleaned.slice(1);
return cleaned;
};
module.exports = { isValidPhone, formatPhone };