Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Street Address validation using Javascript

Tags:

javascript

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

like image 856
gmhk Avatar asked Jul 27 '11 05:07

gmhk


2 Answers

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 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.

like image 178
Jonathan Oliver Avatar answered Sep 19 '22 16:09

Jonathan Oliver


If you want a function for this, try:

function validCharForStreetAddress(c) {
    return ",#-/ !@$%^*(){}|[]\\".indexOf(c) >= 0;
}
like image 28
Ray Toal Avatar answered Sep 19 '22 16:09

Ray Toal