Reputation: 32833
I am trying to validate home address that is street address. But it returns false everytime. Here is my code
validateAddress: function (val) {
console.log('val: ' + val);
var streetregex = /^[a-zA-Z0-9-\/] ?([a-zA-Z0-9-\/]|[a-zA-Z0-9-\/] )*[a-zA-Z0-9-\/]$/;
if ( streetregex.test(val) ) {
console.log('true');
} else {
console.log('false');
}
}
val has street address in this format street name streetnumber, city
.
How can I fix it so it correctly validates my address?
Update
Here is my DEMO
if you give address like this Street name 18, Helsinki
. It returns false whereas I want it to return true for these sort of addresses.
Upvotes: 0
Views: 8525
Reputation: 152027
This regexp will do what you ask for, but I doubt it's useful for any real-life application:
var regexp = /^[\w\s.-]+\d+,\s*[\w\s.-]+$/;
console.log(regexp.test('Main St. 123, New York'));
console.log(regexp.test('123 Wall St., New York'));
Fiddle at http://jsfiddle.net/dandv/fxxTK/5/
The way it works is:
match a sequence of alphanumeric characters, spaces, period or dash, e.g. "Abel-Johnson St."
followed by a number
followed by a comma
followed by another sequence of alphanumeric characters, spaces, period or dash (e.g. "St. Mary-Helen")
This is very frail, however, and you should probably simply not attempt to validate street addresses.
Upvotes: 6