使用 Javascript 验证街道地址

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6839687/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 22:02:26  来源:igfitidea点击:

Street Address validation using Javascript

javascript

提问by gmhk

I am doing a street Address validation, in stret address validation text field should allow all the characters and Special characters.

我正在做街道地址验证,在街道地址验证文本字段中应该允许所有字符和特殊字符。

To allow all the special characters, I have used the following way. Is there a better way to allow all the special characters?

为了允许所有特殊字符,我使用了以下方式。 有没有更好的方法来允许所有特殊字符?

function isAcceptedChar_StAddress(s)
{
    if (s == ',' || s == '#' || s == '-' || s == '/' || s == " " ||  
    s == '!' || s == '@' || s == '$' || s == "%" ||  s == '^' || 
    s == '*' || s == '(' || s == ")" || s == "{" ||  s == '}' || 
    s == '|' || s == '[' || s == "]"  || s == "\")
    {
        return true;
    }
    else
    {
        return false;
    }
}

In the above code, i am comparing each character if it is matching I am returning true, else return false

在上面的代码中,我比较每个字符是否匹配我返回true,否则返回false

回答by Jonathan Oliver

Address validation is an very sticky subject with lots of gotchas. For example, here in the United States you can easily have addresses with a dash "-" and slash "/" characters. For example: 123-A Main Street. Here the "-A" typically indicates an apartment number.

地址验证是一个非常棘手的主题,有很多问题。例如,在美国,您可以轻松获得带有破折号“-”和斜线“/”字符的地址。例如:123-A 大街。这里的“-A”通常表示公寓号。

Furthermore, you can have fractions for streets and apartments, as in "4567 40 1/2 Road", where the name of the street is "40 1/2 Road", so you can't rule out using the slash character.

此外,您可以为街道和公寓使用分数,如“4567 40 1/2 Road”,其中街道名称为“40 1/2 Road”,因此不能排除使用斜杠字符。

The pound/hash "#" character is often used as an apartment level designator. For example, instead of using Suite 409 (often written as STE 409), you could have "# 409".

磅/哈希“#”字符通常用作公寓级别指示符。例如,您可以使用“#409”而不是使用 Suite 409(通常写为 STE 409)。

A bigger question has to be asked in all of this: what is the ultimate objective? Are you trying to see if the address mightbe real? Or do you want to see if the address actually exists?

所有这一切都必须提出一个更大的问题:最终目标是什么?你想看看地址是否真实?或者你想看看地址是否真的存在?

There are a number of third-party solutions available to see if an address is real such as ServerObjects, Melissa Data, and SmartyStreets. There are even fewer that offer full Javascript integration. SmartyStreets offers a Javascript implementation that you can easily plug into your website with their address validation api.

有许多第三方解决方案可用于查看地址是否真实,例如 ServerObjects、Melissa Data 和 SmartyStreets。提供完整 Javascript 集成的甚至更少。SmartyStreets 提供了一个 Javascript 实现,您可以使用其地址验证 api轻松插入您的网站。

In the interest of full disclosure, I am the founder of SmartyStreets and I'd love to hear your thoughts if you end up using an address verification system. You can reach me on Twitter under the name jonathan_oliver.

为了充分披露,我是 SmartyStreets 的创始人,如果您最终使用地址验证系统,我很想听听您的想法。你可以在 Twitter 上以 jonathan_oliver 的名义联系我。

回答by Ray Toal

If you want a function for this, try:

如果您想要一个功能,请尝试:

function validCharForStreetAddress(c) {
    return ",#-/ !@$%^*(){}|[]\".indexOf(c) >= 0;
}

回答by Ibu

Why not use regular expression instead

为什么不改用正则表达式

var regex = /[,#-\/\s\!\@$.....]/gi; // ... add all the characters you need
if (regex.test(s)) {
  return true;
}
return false;

回答by The Scrum Meister

var illegalChars = [',', '#', '-', '/', " ",  
    '!', '@', '$', "%",  '^', 
    '*', '(', ")", "{",  '}', 
    '|', '[', "]" , "\"];

function isAcceptedChar_StAddress(s) {
    for(var i = 0; i < illegalChars.length; i++) {
        if(s == illegalChars[i]) return true;
    }
    return false;
}

Alternatively, using jQuery:

或者,使用 jQuery:

function isAcceptedChar_StAddress(s) {
    return $.inArray(s, illegalChars) != -1;
}

Note: You may want to sort the array and do a binary search.

注意:您可能希望对数组进行排序并进行二分查找。