var cityRegex = /^[a-zA-z] ?([a-zA-z]|[a-zA-z] )*[a-zA-z]$/; is what I tried.
But it errors when you type in a city like "St. Petersburg."
Update: Seems almost like a lost cause. Too many oddly-named cities out there with numbers, dashes, apostrophes, periods, etc.
var cityRegex = /^[a-zA-z] ?([a-zA-z]|[a-zA-z] )*[a-zA-z]$/; is what I tried.
But it errors when you type in a city like "St. Petersburg."
Update: Seems almost like a lost cause. Too many oddly-named cities out there with numbers, dashes, apostrophes, periods, etc.
javascript
jquery
regex
Share
Improve this question
edited Mar 27, 2013 at 22:01user1729506
asked Mar 27, 2013 at 21:53
user1729506user172950697544 gold badges1515 silver badges2929 bronze badges12
3Your best bet is to just validate number of characters and make sure there are no numbers. City names can have dashes as well as prefixes, I wouldn't trust a non forgiving regex for this.
– elclanrs
CommentedMar 27, 2013 at 21:55
4Sounds like a lost cause to me... Perhaps validate it against google map api
– mplungjan
CommentedMar 27, 2013 at 21:55
1What are you hoping to include/exclude with such a filter?
– mayhewr
CommentedMar 27, 2013 at 21:55
6Hill Number 1 is a city, so is Y City and Land O' Lakes - ever been to Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch
– mplungjan
CommentedMar 27, 2013 at 21:56
1Not to forget city names with non-ASCII characters.
– nwellnhof
CommentedMar 27, 2013 at 22:01
|
Show 7 more ments
1 Answer
1
Reset to default
5
If the ments don't make it clear enough, this is not something that can realistically be validated by regex. The correct thing to do here is just accept that there will be some bad data inputted and move along. If you really need the city to exist and you think that this javascript validation will help you, you are sorely mistaken.
In answer to your question, the correct validation here is: