如何验证国际邮政编码请快速回复 [英] How to validate the zip code for international please reply fast

查看:98
本文介绍了如何验证国际邮政编码请快速回复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用正则表达式或代码验证和限制所有国家/地区的邮政编码验证的文本字段



我尝试过:



How to validate and restrict text field for zip code validation for all countries using regular expression or by code

What I have tried:

function getAddressInfoByZip(zip)
    {
        debugger
        //var zip = "500085";
        var key = "AIzaSyBFIZDRp_iKUPk71jCyUbmMTmBzdhWGj4s";

        if (zip.length != 0) {
            var addr = {};
            try {
                $.getJSON("https://maps.googleapis.com/maps/api/geocode/json?", { 'address': zip }, function (data, status) {
                    debugger
                    var results = data.results;
                    var status = data.status;
                    if(status == "ZERO_RESULTS" ||status=="OVER_QUERY_LIMIT"){
                        ValidateZipcodeBybasedonEntry(0,@Html.Raw(Json.Encode(Focus.Resources.msgValidateZipCodeforCountry)));
                        return;
                    }
                    if (status == "OK") {
                        if (results.length >= 1) {
                            for (var ii = 0; ii < results[0].address_components.length; ii++) {
                                var street_number = route = street = city = state = zipcode = country = formatted_address = '';
                                var types = results[0].address_components[ii].types.join(",");
                                //if (types == "street_number") {
                                //    addr.street_number = results[0].address_components[ii].long_name;
                                //}
                                //if (types == "route" || types == "point_of_interest,establishment") {
                                //    addr.route = results[0].address_components[ii].long_name;
                                //}
                                if (types == "sublocality,political" || types == "locality,political" || types == "neighborhood,political" || types == "administrative_area_level_3,political") {
                                    addr.city = (city == '' || types == "locality,political") ? results[0].address_components[ii].long_name : city;
                                }
                                if (types == "administrative_area_level_1,political") {
                                    addr.state = results[0].address_components[ii].short_name;
                                }
                                //if (types == "postal_code" || types == "postal_code_prefix,postal_code") {
                                //    addr.zipcode = results[0].address_components[ii].long_name;
                                //}
                                if (types == "country,political") {
                                    addr.country = results[0].address_components[ii].long_name;
                                }
                            } }
                        if (addr.country!=null && addr.country!=undefined) {
                            var eleCountry = document.getElementById('drpCountry');
                            var eleCountryText = eleCountry.options[eleCountry.selectedIndex].text
                            if(eleCountryText != ""){
                                if(eleCountryText.toLowerCase() != addr.country.toLowerCase()){
                                    ValidateZipcodeBybasedonEntry(1,@Html.Raw(Json.Encode(Focus.Resources.msgValidateZipCodeforCountry)));
                                    return;
                                }
                            }
                        }
                        if (addr!=null&& addr!=undefined) {
                            if(addr.state!=undefined) {
                                if(document.getElementById('txtState').value != ""){
                                    if(document.getElementById('txtState').value.toLowerCase() != addr.state.toLowerCase()){
                                        ValidateZipcodeBybasedonEntry(2,@Html.Raw(Json.Encode(Focus.Resources.msgValidateZipCodeforState)));
                                        return;
                                    }
                                }
                            }
                            if (addr.city!=undefined) {
                                if(document.getElementById('txtCity').value != ""){
                                    if(document.getElementById('txtCity').value.toLowerCase() != addr.city.toLowerCase()){
                                        ValidateZipcodeBybasedonEntry(3,@Html.Raw(Json.Encode(Focus.Resources.msgValidateZipCodeforCity)));
                                        return;
                                    }
                                }
                            }
                        }
                    }
                });
            }
            catch (e) {
                debugger
                alert(e.message);
            }
        }
    }

推荐答案

.getJSON(https://maps.googleapis .com / maps / api / geocode / json?,{'address':zip},function(data,status){
debugger
var results = data.results;
var status = data.status;
if(status ==ZERO_RESULTS|| status ==OVER_QUERY_LIMIT){
ValidateZipcodeBybasedonEntry(0,@ Html.Raw(Json.Encode(Focus.Resources.msgValidateZipCodeforCountry) ));
return;
}
if(status ==OK){
if(results.length> = 1){
for(var ii = 0; ii< results [0] .address_components.length; ii ++){
var street_number = route = street = city = state = zipcode = country = formatted_address ='';
var types = results [0] .address_components [ii] .types.join(,);
// if(types ==street_number){
// addr.street_number = results [0] .address_components [ii] .long_name;
//}
// if(types ==route|| types ==point_of_interest,establishment){
// addr.route = results [0] .address_components [ II] .long_name;
//}
if(types ==sublocality,political|| types ==locality,political|| types ==neighborhood,political|| types ==administrative_area_level_3,political ){
addr.city =(city ==''|| types ==locality,political)?结果[0] .address_components [ii] .long_name:city;
}
if(types ==administrative_area_level_1,political){
addr.state = results [0] .address_components [ii] .short_name;
}
// if(types ==postal_code|| types ==postal_code_prefix,postal_code){
// addr.zipcode = results [0] .address_components [ii] .long_name;
//}
if(types ==country,political){
addr.country = results [0] .address_components [ii] .long_name;
}
}}
if(addr.country!= null&& addr.country!= undefined){
var eleCountry = document.getElementById('drpCountry') ;
var eleCountryText = eleCountry.options [eleCountry.selectedIndex] .text
if(eleCountryText!=){
if(eleCountryText.toLowerCase()!= addr.country.toLowerCase() ){
ValidateZipcodeBybasedonEntry(1,@ Html.Raw(Json.Encode(Focus.Resources.msgValidateZipCodeforCountry)));
返回;
}
}
}
if(addr!= null&& addr!= undefined){
if(addr.state!= undefined){
if(document.getElementById('txtState')。value!=){
if(document.getElementById('txtState')。value.toLowerCase()!= addr.state.toLowerCase()) {
ValidateZipcodeBybasedonEntry(2,@ Html.Raw(Json.Encode(Focus.Resources.msgValidateZipCodeforState)));
返回;
}
}
}
if(addr.city!= undefined){
if(document.getElementById('txtCity')。value!=) {
if(document.getElementById('txtCity')。value.toLowerCase()!= addr.city.toLowerCase()){
ValidateZipcodeBybasedonEntry(3,@ Html.Raw(Json.Encode(Focus) .Resources.msgValidateZipCodeforCity)));
返回;
}
}
}
}
}
});
}
catch(e){
debugger
alert(e.message);
}
}
}
.getJSON("https://maps.googleapis.com/maps/api/geocode/json?", { 'address': zip }, function (data, status) { debugger var results = data.results; var status = data.status; if(status == "ZERO_RESULTS" ||status=="OVER_QUERY_LIMIT"){ ValidateZipcodeBybasedonEntry(0,@Html.Raw(Json.Encode(Focus.Resources.msgValidateZipCodeforCountry))); return; } if (status == "OK") { if (results.length >= 1) { for (var ii = 0; ii < results[0].address_components.length; ii++) { var street_number = route = street = city = state = zipcode = country = formatted_address = ''; var types = results[0].address_components[ii].types.join(","); //if (types == "street_number") { // addr.street_number = results[0].address_components[ii].long_name; //} //if (types == "route" || types == "point_of_interest,establishment") { // addr.route = results[0].address_components[ii].long_name; //} if (types == "sublocality,political" || types == "locality,political" || types == "neighborhood,political" || types == "administrative_area_level_3,political") { addr.city = (city == '' || types == "locality,political") ? results[0].address_components[ii].long_name : city; } if (types == "administrative_area_level_1,political") { addr.state = results[0].address_components[ii].short_name; } //if (types == "postal_code" || types == "postal_code_prefix,postal_code") { // addr.zipcode = results[0].address_components[ii].long_name; //} if (types == "country,political") { addr.country = results[0].address_components[ii].long_name; } } } if (addr.country!=null && addr.country!=undefined) { var eleCountry = document.getElementById('drpCountry'); var eleCountryText = eleCountry.options[eleCountry.selectedIndex].text if(eleCountryText != ""){ if(eleCountryText.toLowerCase() != addr.country.toLowerCase()){ ValidateZipcodeBybasedonEntry(1,@Html.Raw(Json.Encode(Focus.Resources.msgValidateZipCodeforCountry))); return; } } } if (addr!=null&& addr!=undefined) { if(addr.state!=undefined) { if(document.getElementById('txtState').value != ""){ if(document.getElementById('txtState').value.toLowerCase() != addr.state.toLowerCase()){ ValidateZipcodeBybasedonEntry(2,@Html.Raw(Json.Encode(Focus.Resources.msgValidateZipCodeforState))); return; } } } if (addr.city!=undefined) { if(document.getElementById('txtCity').value != ""){ if(document.getElementById('txtCity').value.toLowerCase() != addr.city.toLowerCase()){ ValidateZipcodeBybasedonEntry(3,@Html.Raw(Json.Encode(Focus.Resources.msgValidateZipCodeforCity))); return; } } } } } }); } catch (e) { debugger alert(e.message); } } }


没有办法实现这一目标。每个国家/地区都有自定义邮政编码系统。

请参阅: zipcode - 什么是终极邮政编码和zip正则表达式? - 堆栈溢出 [ ^ ]
There's no way to achieve that. Each country has custom Zip code system.
See: zipcode - What is the ultimate postal code and zip regex? - Stack Overflow[^]


这篇关于如何验证国际邮政编码请快速回复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆