admin管理员组文章数量:1201411
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
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
Share Improve this question asked Jul 27, 2011 at 5:12 gmhkgmhk 15.9k29 gold badges91 silver badges112 bronze badges4 Answers
Reset to default 11Address 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.
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.
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".
A bigger question has to be asked in all of this: what is the ultimate objective? Are you trying to see if the address might be 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. It's called LiveAddress.
In the interest of full disclosure, I am the founder of SmartyStreets.
If you want a function for this, try:
function validCharForStreetAddress(c) {
return ",#-/ !@$%^*(){}|[]\\".indexOf(c) >= 0;
}
Why not use regular expression instead
var regex = /[,#-\/\s\!\@\$.....]/gi; // ... add all the characters you need
if (regex.test(s)) {
return true;
}
return false;
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:
function isAcceptedChar_StAddress(s) {
return $.inArray(s, illegalChars) != -1;
}
Note: You may want to sort the array and do a binary search.
本文标签: Street Address validation using JavascriptStack Overflow
版权声明:本文标题:Street Address validation using Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738555832a2098266.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论