UNPKG

@valkyriestudios/validator

Version:

A lightweight configurable javascript validator

21 lines (20 loc) 662 B
const RGX = /^\+?\d{0,4}[-.\s]?\(?\d{1,3}\)?[-.\s]?\d{1,4}[-.\s]?\d{1,4}[-.\s]?\d{1,9}$/; const RGX_DIGIT = /\d/g; const RGX_PARTS = /(\.|-)/g; function vPhone(val) { if (typeof val !== 'string') return false; if ((val.match(RGX_DIGIT) || []).length < 5) return false; const sparts = val.replace(RGX_PARTS, ' ').split(' '); for (const el of sparts) { const first = el[0]; const last = el[el.length - 1]; if (first === '(' && last !== ')') return false; if (last === ')' && first !== '(') return false; } return RGX.test(val); } export { vPhone, vPhone as default };