6

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

4 Answers 4

11

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.

Sign up to request clarification or add additional context in comments.

Comments

6

If you want a function for this, try:

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

Comments

4

Why not use regular expression instead

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

1 Comment

Becauase a string version using indexOf is faster. The g flag is not required. Can also just return regex.test(s);
0
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.

1 Comment

why not just do if (illegalChars.search(s) > -1) instead of a for loop?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.