PageSourceSearch

https://4buyn.com/public/assets/backend/libs/intl-tel-input/js/intlTelInout-validation.js

js 4buyn.com collected 2026-09-24 12:48:16 UTC 20,752 bytes, 524 lines download raw bytes

1const itiInstances = new Map();
2
3const mobileExamples = {
4    af: "+93700123456",      // Afghanistan
5    al: "+355691234567",     // Albania
6    dz: "+213661234567",     // Algeria
7    as: "+16842501234",      // American Samoa
8    ad: "+376312345",        // Andorra
9    ao: "+244923123456",     // Angola
10    ai: "+12641234567",      // Anguilla
11    ag: "+12681234567",      // Antigua and Barbuda
12    ar: "+5491123456789",    // Argentina
13    am: "+37477123456",      // Armenia
14    aw: "+2971234567",       // Aruba
15    au: "+61412345678",      // Australia
16    at: "+436601234567",     // Austria
17    az: "+994501234567",     // Azerbaijan
18    bs: "+12421234567",      // Bahamas
19    bh: "+97336001234",      // Bahrain
20    bd: "+8801712345678",    // Bangladesh
21    bb: "+12462345678",      // Barbados
22    by: "+375291234567",     // Belarus
23    be: "+32475123456",      // Belgium
24    bz: "+5016123456",       // Belize
25    bj: "+22991234567",      // Benin
26    bm: "+14412345678",      // Bermuda
27    bt: "+97517123456",      // Bhutan
28    bo: "+59171234567",      // Bolivia
29    ba: "+38761234567",      // Bosnia and Herzegovina
30    bw: "+26771234567",      // Botswana
31    br: "+5511912345678",    // Brazil
32    io: "+2461234567",       // British Indian Ocean Territory
33    bn: "+6737123456",       // Brunei Darussalam
34    bg: "+359881234567",     // Bulgaria
35    bf: "+22670123456",      // Burkina Faso
36    bi: "+25771234567",      // Burundi
37    kh: "+85512345678",      // Cambodia
38    cm: "+237650123456",     // Cameroon
39    ca: "+14161234567",      // Canada
40    cv: "+2389912345",       // Cape Verde
41    ky: "+13451234567",      // Cayman Islands
42    cf: "+23675012345",      // Central African Republic
43    td: "+23565012345",      // Chad
44    cl: "+56912345678",      // Chile
45    cn: "+8613712345678",    // China
46    cx: "+61891620000",      // Christmas Island
47    cc: "+61891620000",      // Cocos (Keeling) Islands
48    co: "+573001234567",     // Colombia
49    km: "+2693212345",       // Comoros
50    cd: "+243810123456",     // Congo (DRC)
51    cg: "+2420551234567",    // Congo
52    ck: "+68212345",         // Cook Islands
53    cr: "+50683123456",      // Costa Rica
54    ci: "+2250701234567",    // Côte d'Ivoire
55    hr: "+385912345678",     // Croatia
56    cu: "+5351234567",       // Cuba
57    cw: "+59991234567",      // Curaçao
58    cy: "+35799123456",      // Cyprus
59    cz: "+420601234567",     // Czech Republic
60    dk: "+4520123456",       // Denmark
61    dj: "+25377123456",      // Djibouti
62    dm: "+17671234567",      // Dominica
63    do: "+18091234567",      // Dominican Republic
64    ec: "+593912345678",     // Ecuador
65    eg: "+201001234567",     // Egypt
66    sv: "+50370123456",      // El Salvador
67    gq: "+240222123456",     // Equatorial Guinea
68    er: "+291712345",        // Eritrea
69    ee: "+37251234567",      // Estonia
70    et: "+251911234567",     // Ethiopia
71    fk: "+50012345",         // Falkland Islands
72    fo: "+298321234",        // Faroe Islands
73    fj: "+6797123456",       // Fiji
74    fi: "+358401234567",     // Finland
75    fr: "+33612345678",      // France
76    gf: "+594694123456",     // French Guiana
77    pf: "+68987123456",      // French Polynesia
78    ga: "+24107123456",      // Gabon
79    gm: "+2207123456",       // Gambia
80    ge: "+995551234567",     // Georgia
81    de: "+4915123456789",    // Germany
82    gh: "+233241234567",     // Ghana
83    gi: "+35020012345",      // Gibraltar
84    gr: "+306912345678",     // Greece
85    gl: "+299321234",        // Greenland
86    gd: "+14732345678",      // Grenada
87    gp: "+590690123456",     // Guadeloupe
88    gu: "+16714812345",      // Guam
89    gt: "+50241234567",      // Guatemala
90    gg: "+441481123456",     // Guernsey
91    gn: "+224621234567",     // Guinea
92    gw: "+24599123456",      // Guinea-Bissau
93    gy: "+5926001234",       // Guyana
94    ht: "+50937123456",      // Haiti
95    hn: "+50499123456",      // Honduras
96    hk: "+85291234567",      // Hong Kong
97    hu: "+36701234567",      // Hungary
98    is: "+3546612345",       // Iceland
99    in: "+919000000000",     // India
100    id: "+628123456789",     // Indonesia
101    ir: "+989121234567",     // Iran
102    iq: "+9647712345678",    // Iraq
103    ie: "+353851234567",     // Ireland
104    im: "+441624123456",     // Isle of Man
105    il: "+972501234567",     // Israel
106    it: "+393401234567",     // Italy
107    jm: "+18761234567",      // Jamaica
108    jp: "+819012345678",     // Japan
109    je: "+441534123456",     // Jersey
110    jo: "+962791234567",     // Jordan
111    kz: "+77712345678",      // Kazakhstan
112    ke: "+254712345678",     // Kenya
113    ki: "+68612345",         // Kiribati
114    kw: "+96551234567",      // Kuwait
115    kg: "+996701234567",     // Kyrgyzstan
116    la: "+8562012345678",    // Laos
117    lv: "+37120123456",      // Latvia
118    lb: "+96170123456",      // Lebanon
119    ls: "+26651234567",      // Lesotho
120    lr: "+231770123456",     // Liberia
121    ly: "+218912345678",     // Libya
122    li: "+4231234567",       // Liechtenstein
123    lt: "+37061234567",      // Lithuania
124    lu: "+352621234567",     // Luxembourg
125    mo: "+85361234567",      // Macau
126    mk: "+38970123456",      // North Macedonia
127    mg: "+261320123456",     // Madagascar
128    mw: "+265991234567",     // Malawi
129    my: "+60123456789",      // Malaysia
130    mv: "+9607123456",       // Maldives
131    ml: "+22370123456",      // Mali
132    mt: "+35699123456",      // Malta
133    mh: "+6924912345",       // Marshall Islands
134    mq: "+596696123456",     // Martinique
135    mr: "+22240123456",      // Mauritania
136    mu: "+23057212345",      // Mauritius
137    yt: "+262639012345",     // Mayotte
138    mx: "+5215512345678",    // Mexico
139    fm: "+6913201234",       // Micronesia
140    md: "+37369123456",      // Moldova
141    mc: "+37761234567",      // Monaco
142    mn: "+97699123456",      // Mongolia
143    me: "+38260123456",      // Montenegro
144    ms: "+16642345678",      // Montserrat
145    ma: "+212612345678",     // Morocco
146    mz: "+258841234567",     // Mozambique
147    mm: "+959251234567",     // Myanmar
148    na: "+264811234567",     // Namibia
149    nr: "+6745551234",       // Nauru
150    np: "+9779812345678",    // Nepal
151    nl: "+31612345678",      // Netherlands
152    nc: "+687123456",        // New Caledonia
153    nz: "+64211234567",      // New Zealand
154    ni: "+50585123456",      // Nicaragua
155    ne: "+22790123456",      // Niger
156    ng: "+2348012345678",    // Nigeria
157    nu: "+6831234",          // Niue
158    nf: "+67231234",         // Norfolk Island
159    kp: "+8501912345678",    // North Korea
160    mp: "+16704812345",      // Northern Mariana Islands
161    no: "+4791234567",       // Norway
162    om: "+96891234567",      // Oman
163    pk: "+923001234567",     // Pakistan
164    pw: "+6805551234",       // Palau
165    ps: "+970599123456",     // Palestine
166    pa: "+5076012345",       // Panama
167    pg: "+67572123456",      // Papua New Guinea
168    py: "+595981234567",     // Paraguay
169    pe: "+51981234567",      // Peru
170    ph: "+639171234567",     // Philippines
171    pl: "+48500123456",      // Poland
172    pt: "+351912345678",     // Portugal
173    pr: "+17871234567",      // Puerto Rico
174    qa: "+97450123456",      // Qatar
175    re: "+262692123456",     // Réunion
176    ro: "+40712345678",      // Romania
177    ru: "+79261234567",      // Russia
178    rw: "+250788123456",     // Rwanda
179    bl: "+590690123456",     // Saint Barthélemy
180    sh: "+29012345",         // Saint Helena
181    kn: "+18692234567",      // Saint Kitts and Nevis
182    lc: "+17582345678",      // Saint Lucia
183    mf: "+590690123456",     // Saint Martin
184    pm: "+508123456",        // Saint Pierre and Miquelon
185    vc: "+17841234567",      // Saint Vincent and the Grenadines
186    ws: "+6857012345",       // Samoa
187    sm: "+37866123456",      // San Marino
188    st: "+2399912345",       // São Tomé and Príncipe
189    sa: "+966501234567",     // Saudi Arabia
190    sn: "+221701234567",     // Senegal
191    rs: "+381601234567",     // Serbia
192    sc: "+2482512345",       // Seychelles
193    sl: "+23299123456",      // Sierra Leone
194    sg: "+6581234567",       // Singapore
195    sx: "+17219876543",      // Sint Maarten
196    sk: "+421901234567",     // Slovakia
197    si: "+38640123456",      // Slovenia
198    sb: "+6777123456",       // Solomon Islands
199    so: "+252612345678",     // Somalia
200    za: "+27821234567",      // South Africa
201    kr: "+821012345678",     // South Korea
202    ss: "+211912345678",     // South Sudan
203    es: "+34600123456",      // Spain
204    lk: "+94771234567",      // Sri Lanka
205    sd: "+249912345678",     // Sudan
206    sr: "+5979123456",       // Suriname
207    sz: "+26876123456",      // Eswatini (Swaziland)
208    se: "+46701234567",      // Sweden
209    ch: "+41791234567",      // Switzerland
210    sy: "+963912345678",     // Syria
211    tw: "+886912345678",     // Taiwan
212    tj: "+992901234567",     // Tajikistan
213    tz: "+255712345678",     // Tanzania
214    th: "+66812345678",      // Thailand
215    tl: "+67077234567",      // Timor-Leste
216    tg: "+22890123456",      // Togo
217    tk: "+6901234",          // Tokelau
218    to: "+6767123456",       // Tonga
219    tt: "+18681234567",      // Trinidad and Tobago
220    tn: "+21620123456",      // Tunisia
221    tr: "+905301234567",     // Turkey
222    tm: "+99361234567",      // Turkmenistan
223    tc: "+16492123456",      // Turks and Caicos Islands
224    tv: "+6889123456",       // Tuvalu
225    ug: "+256701234567",     // Uganda
226    ua: "+380501234567",     // Ukraine
227    ae: "+971501234567",     // United Arab Emirates
228    gb: "+447912345678",     // United Kingdom
229    us: "+12025550123",      // United States
230    uy: "+59891234567",      // Uruguay
231    uz: "+998901234567",     // Uzbekistan
232    vu: "+6785012345",       // Vanuatu
233    va: "+37912345678",      // Vatican City
234    ve: "+584121234567",     // Venezuela
235    vn: "+84981234567",      // Vietnam
236    wf: "+681123456",       // Wallis and Futuna
237    ye: "+967712345678",     // Yemen
238    zm: "+260971234567",     // Zambia
239    zw: "+263712345678"      // Zimbabwe
240};
241
242function initializeIntlTelInput() {
243    const inputs = document.querySelectorAll(
244        'input[type="tel"]:not([data-intl-initialized])'
245    );
246
247    const defaultCountryCodeElement = document.querySelector(".system-default-country-code");
248    const defaultCountry = defaultCountryCodeElement?.dataset?.value?.toLowerCase() || "bd";
249
250    inputs.forEach(input => {
251        const iti = window.intlTelInput(input, {
252            initialCountry: defaultCountry,
253            autoInsertDialCode: false,
254            nationalMode: false,
255            formatOnDisplay: false,
256            separateDialCode: false,
257            showSelectedDialCode: true,
258            autoPlaceholder: "off",
259            utilsScript:
260                "https://cdn.jsdelivr.net/npm/[email protected]/build/js/utils.js"
261        });
262
263        // Override _setFlag after init
264        const originalSetFlag = iti._setFlag;
265        iti._setFlag = function(countryCode) {
266            const result = originalSetFlag.call(this, countryCode);
267
268            if (this.options.showSelectedDialCode && this.isRTL) {
269                const selectedFlagWidth =
270                    this.selectedFlag.offsetWidth ||
271                    this._getHiddenSelectedFlagWidth();
272                this.telInput.style.paddingLeft = `${selectedFlagWidth + 6}px`;
273                this.telInput.style.paddingRight = "";
274            }
275
276            return result;
277        };
278
279        iti._setFlag(iti.getSelectedCountryData().iso2);
280
281        // itiInstances.set(input, iti);
282        // input.setAttribute("data-intl-initialized", "true");
283
284        // ✅ Move name & value to a hidden input
285        const originalName = input.getAttribute('name');
286        const originalValue = input.value;
287        input.removeAttribute('name'); // remove name from visible input
288
289        const hiddenInput = document.createElement('input');
290        hiddenInput.type = 'hidden';
291        hiddenInput.name = originalName;
292        input.parentNode.appendChild(hiddenInput);
293
294        try {
295            const originalSetter = Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, 'value').set;
296            const originalGetter = Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, 'value').get;
297
298            Object.defineProperty(hiddenInput, 'value', {
299                set(newValue) {
300                    originalSetter.call(this, newValue);
301                    iti.setNumber(newValue); // uses intlTelInput to format & update country
302                },
303                get() {
304                    return originalGetter.call(this);
305                }
306            });
307        } catch (e) {
308        }
309
310        const hiddenCountryCodeInput = document.createElement('input');
311        hiddenCountryCodeInput.type = 'hidden';
312        hiddenCountryCodeInput.name = 'country_code';
313        input.parentNode.appendChild(hiddenCountryCodeInput);
314
315        // ✅ Set initial value if exists
316        if (originalValue) {
317            iti.setNumber(originalValue);
318            hiddenInput.value = iti.getNumber(); // formatted full number
319            hiddenCountryCodeInput.value = iti?.getSelectedCountryData()?.dialCode;
320
321            // Set data-value and data-country
322            input.setAttribute('data-value', iti.getNumber());
323            input.setAttribute('data-country', iti.getSelectedCountryData()?.iso2);
324        }
325
326        // ✅ Keep updating hidden input on user change
327        input.addEventListener('input', () => {
328            hiddenInput.value = iti.getNumber();
329            hiddenCountryCodeInput.value = iti?.getSelectedCountryData()?.dialCode;
330        });
331
332        input.addEventListener('countrychange', () => {
333            hiddenInput.value = iti.getNumber();
334            hiddenCountryCodeInput.value = iti?.getSelectedCountryData()?.dialCode;
335        });
336
337        iti._setFlag(iti.getSelectedCountryData().iso2);
338        itiInstances.set(input, iti);
339        input.setAttribute("data-intl-initialized", "true");
340    });
341}
342
343function keepOnlyNumbers(str) {
344    return str.replace(/\D/g, "");
345}
346
347function getLocalExampleNumberLength(iti) {
348    try {
349        const iso2 = iti.getSelectedCountryData().iso2;
350        let dialCode = iti.getSelectedCountryData().dialCode;
351
352        if (!iso2 || !dialCode) {
353            console.warn("No country or dial code found");
354            return 12; // fallback
355        }
356
357        dialCode = dialCode.toString();
358
359        // Use example from your predefined list or intl-tel-input utils fallback
360        let example = mobileExamples[iso2];
361        if (!example && window.intlTelInputUtils) {
362            example = intlTelInputUtils.getExampleNumber(
363                iso2,
364                true,
365                intlTelInputUtils.numberFormat.E164
366            );
367        }
368
369        if (!example) {
370            console.warn("No example number found for", iso2);
371            return 12; // fallback
372        }
373
374        const digitsOnly = example.replace(/\D/g, "");
375
376        // Check if dialCode is at start of digitsOnly
377        const dialCodeIndex = digitsOnly.startsWith(dialCode) ? 0 : -1;
378
379        const nationalDigits =
380            dialCodeIndex === 0
381                ? digitsOnly.slice(dialCode.length)
382                : digitsOnly; // fallback to full if dialCode not found at start
383
384        return nationalDigits.length;
385    } catch (e) {
386        console.warn("Fallback to default local max length (12)", e);
387        return 12;
388    }
389}
390
391function validatePhoneInput(input) {
392    const iti = itiInstances.get(input);
393    if (!iti || typeof iti.isValidNumber !== "function") return false;
394
395    const fullNumber = iti.getNumber(); // E164 format
396    const dialCode = iti.getSelectedCountryData().dialCode;
397    const digitsOnly = keepOnlyNumbers(fullNumber);
398    const dialCodeIndex = digitsOnly.indexOf(dialCode);
399    const localNumber =
400        dialCodeIndex >= 0
401            ? digitsOnly.slice(dialCodeIndex + dialCode.length)
402            : digitsOnly;
403
404    const expectedLength = getLocalExampleNumberLength(iti);
405    const isValid = iti.isValidNumber() && localNumber.length === expectedLength;
406
407    input.classList.toggle("is-valid", isValid);
408    input.classList.toggle("is-invalid", !isValid);
409
410    return isValid;
411}
412
413document.addEventListener("DOMContentLoaded", () => {
414    initializeIntlTelInput();
415
416    document.addEventListener("input", function (e) {
417        if (e.target.matches('input[type="tel"]')) {
418            const input = e.target;
419            const iti = itiInstances.get(input);
420            if (!iti) return;
421
422            const inputVal = keepOnlyNumbers(input.value);
423            const dialCode = iti.getSelectedCountryData().dialCode;
424            const expectedLength = getLocalExampleNumberLength(iti);
425
426            // Extract local part (remove dial code if present)
427            const dialCodeIndex = inputVal.indexOf(dialCode);
428            let localPart =
429                dialCodeIndex >= 0
430                    ? inputVal.slice(dialCodeIndex + dialCode.length)
431                    : inputVal;
432
433            // Limit only if too long
434            if (localPart.length > expectedLength) {
435                localPart = localPart.slice(0, expectedLength + 4);
436                input.value = localPart; // update field only when needed
437            }
438
439            // Update hidden input fields
440            const hiddenInput = input.parentNode.querySelector('input[type="hidden"]:not([name="country_code"])');
441            if (hiddenInput && iti) {
442                hiddenInput.value = iti.getNumber();
443            }
444
445            const hiddenCountryCodeInput = input.parentNode.querySelector('input[name="country_code"]');
446            if (hiddenCountryCodeInput && iti) {
447                hiddenCountryCodeInput.value = iti.getSelectedCountryData()?.dialCode || "";
448            }
449
450            validatePhoneInput(input);
451        }
452    });
453
454    document.querySelectorAll('form').forEach(form => {
455        form.addEventListener('reset', function () {
456            setTimeout(() => {
457                const phoneInputs = form.querySelectorAll('input[type="tel"]');
458
459                phoneInputs.forEach(input => {
460                    const iti = itiInstances.get(input);
461                    if (!iti) return;
462
463                    // Read from data-* attributes
464                    const dataValue = input.getAttribute('data-value') || "";
465                    const dataCountry = input.getAttribute('data-country') || "bd";
466
467                    if (dataValue) {
468                        iti.setCountry(dataCountry);       // set country first
469                        iti.setNumber(dataValue);          // set full number
470                        input.value = iti.getNumber();     // update visible input
471                    } else {
472                        iti.setCountry(dataCountry);
473                        iti.setNumber("");
474                        input.value = "";
475                    }
476
477                    // Update hidden input
478                    const hiddenInput = input.parentNode.querySelector('input[type="hidden"]:not([name="country_code"])');
479                    if (hiddenInput) hiddenInput.value = iti.getNumber();
480
481                    // Update hidden country code input
482                    const hiddenCountryCodeInput = input.parentNode.querySelector('input[name="country_code"]');
483                    if (hiddenCountryCodeInput) hiddenCountryCodeInput.value = iti.getSelectedCountryData()?.dialCode || "";
484
485                    // Re-set data-* attributes from current input state
486                    input.setAttribute('data-value', iti.getNumber());
487                    input.setAttribute('data-country', iti.getSelectedCountryData()?.iso2 || "");
488
489                    input.classList.remove("is-valid", "is-invalid");
490                });
491            }, 0); // Wait for native reset to happen
492        });
493    });
494});
495
496
497try {
498    // observer to watch for added inputs
499    const observer = new MutationObserver(function(mutationsList) {
500        mutationsList.forEach(function(mutation) {
501            mutation.addedNodes.forEach(function(node) {
502                if (node.nodeType === 1) { // element
503                    if (node.matches('input[type="tel"]')) {
504                        initIntlTelInputOn(node);
505                    } else {
506                        // also check descendants
507                        node.querySelectorAll && node.querySelectorAll('input[type="tel"]').forEach(initIntlTelInputOn);
508                    }
509                }
510            });
511        });
512    });
513
514    observer.observe(document.body, { childList: true, subtree: true });
515
516    function initIntlTelInputOn(input) {
517        if (!input.hasAttribute('data-intl-initialized')) {
518            initializeIntlTelInput();
519        }
520    }
521
522} catch (e) {
523
524}

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.