开发者

Street Address validation using Javascript

开发者 https://www.devze.com 2023-03-23 17:14 出处:网络
I am doing a street Address validation, in stret address validation text field should allow all the characters and Special characters.

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 c开发者_高级运维ode, i am comparing each character if it is matching I am returning true, else return false


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.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消