PageSourceSearch

https://globaljournals.org/core/views/b68adeb50e/form-add-fiels/address-autofill.js?rnd=90998

js globaljournals.org collected 2026-10-01 11:47:36 UTC 3,979 bytes, 99 lines download raw bytes

1jQuery(document).ready(function ($) {
2
3    // Manual Address Parser (Smart Guesser)
4    function parseAddress(fullAddress) {
5        if (!fullAddress) return;
6
7        var result = {
8            city: '',
9            state: '',
10            zip: '',
11            country: ''
12        };
13
14        // 1. Try to find Pincode/Zip (6 digits for India, 5 for US)
15        var zipMatch = fullAddress.match(/\b\d{6}\b/) || fullAddress.match(/\b\d{5}\b/);
16        if (zipMatch) {
17            result.zip = zipMatch[0];
18            // Remove zip from string to avoid confusion
19            fullAddress = fullAddress.replace(result.zip, '').trim();
20        }
21
22        // 2. Split by commas to analyze parts
23        // Example: "Indore, Madhya Pradesh 452009" -> ["Indore", "Madhya Pradesh"]
24        var parts = fullAddress.split(',').map(function (item) {
25            return item.trim();
26        }).filter(function (item) {
27            return item !== '';
28        });
29
30        if (parts.length > 0) {
31            // Assume the last part is State (if not country)
32            // Common format: [Street], [City], [State]
33
34            // Check known countries in the last part
35            var countries = ['India', 'USA', 'United States', 'UK', 'United Kingdom', 'Canada', 'Australia'];
36            var lastPart = parts[parts.length - 1];
37
38            var foundCountry = countries.find(c => lastPart.toLowerCase().includes(c.toLowerCase()));
39
40            if (foundCountry) {
41                result.country = foundCountry;
42                parts.pop(); // Remove country from parts
43            } else {
44                // Default to India if typical Indian states found or just empty
45                result.country = "India";
46            }
47
48            // Now look at remaining parts
49            if (parts.length >= 1) {
50                // Last item is likely State
51                result.state = parts[parts.length - 1];
52            }
53            if (parts.length >= 2) {
54                // Second to last is likely City
55                result.city = parts[parts.length - 2];
56            } else if (parts.length === 1) {
57                // If only one part remains, usually it's City (if state was missing) or State
58                // Let's assume matches 100% logic of user example: "Indore, Madhya Pradesh"
59            }
60
61            // Refined Logic for User's specific example: 
62            // "3130, Sudama Nagar..., Indore, Madhya Pradesh"
63            // Last part: "Madhya Pradesh" (State)
64            // Second last: "Indore" (City)
65        }
66
67        return result;
68    }
69
70    // Trigger on Blur (when user leaves the field)
71    $('#shipping_address, input[name="shipping_address"]').on('blur', function () {
72        var val = $(this).val();
73        if (val.length < 5) return;
74
75        console.log("Parsing address: " + val);
76        var parsed = parseAddress(val);
77
78        if (parsed) {
79            // Auto-fill fields if they are empty
80            var $cityProps = $('#shipping_city, input[name="shipping_city"]');
81            var $stateProps = $('#shipping_state, input[name="shipping_state"]');
82            var $zipProps = $('#shipping_zip, input[name="shipping_zip"]');
83            var $countryProps = $('#shipping_country, select[name="shipping_country"]');
84
85            if (parsed.city && !$cityProps.val()) $cityProps.val(parsed.city);
86            if (parsed.state && !$stateProps.val()) $stateProps.val(parsed.state);
87            if (parsed.zip && !$zipProps.val()) $zipProps.val(parsed.zip);
88
89            // Country Select Selection
90            if (parsed.country) {
91                $countryProps.find('option').filter(function () {
92                    return $(this).text().toLowerCase() === parsed.country.toLowerCase();
93                }).prop('selected', true);
94
95                if (!$countryProps.val()) $countryProps.val(parsed.country);
96            }
97        }
98    });
99});

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.