UNPKG

@valkyriestudios/validator

Version:

A lightweight configurable javascript validator

24 lines (23 loc) 752 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.vPhone = vPhone; exports.default = vPhone; 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); }