mobile-number-validate
Version:
Indian mobile number validator
2 lines (1 loc) • 1.26 kB
JavaScript
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t(require("libphonenumber-js")):"function"==typeof define&&define.amd?define(["libphonenumber-js"],t):(e="undefined"!=typeof globalThis?globalThis:e||self).MobileValidator=t(e.libphonenumber)}(this,(function(e){"use strict";function t(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var i=t(e),n={exports:{}};return function(e){const{parsePhoneNumber:t,isValidPhoneNumber:n}=i.default;class o{constructor(){this.init()}init(){"undefined"!=typeof document&&document.addEventListener("DOMContentLoaded",(()=>{document.querySelectorAll(".mobile-validate").forEach((e=>{e.addEventListener("blur",this.validate.bind(this))}))}))}validate(e){const i=e.target,o=i.value;try{const e=t(o,"IN");if(!n(o,"IN")||"MOBILE"!==e.getType()||!this.isIndianMobileNumber(o))throw new Error("Invalid Indian mobile number");i.setCustomValidity(""),i.classList.remove("invalid"),i.classList.add("valid")}catch(e){i.setCustomValidity("Invalid Indian mobile number"),i.classList.remove("valid"),i.classList.add("invalid")}}isIndianMobileNumber(e){return/^[6-9]\d{9}$/.test(e.replace(/\D/g,""))}}e.exports&&(e.exports=o),"undefined"!=typeof window&&(window.MobileValidator=o)}(n),n.exports}));