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.
Share Improve this question edited Mar 27, 2013 at 22:01 user1729506 asked Mar 27, 2013 at 21:53 user1729506user1729506 9754 gold badges15 silver badges29 bronze badges 12- 3 Your 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 Commented Mar 27, 2013 at 21:55
- 4 Sounds like a lost cause to me... Perhaps validate it against google map api – mplungjan Commented Mar 27, 2013 at 21:55
- 1 What are you hoping to include/exclude with such a filter? – mayhewr Commented Mar 27, 2013 at 21:55
- 6 Hill Number 1 is a city, so is Y City and Land O' Lakes - ever been to Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch – mplungjan Commented Mar 27, 2013 at 21:56
- 1 Not to forget city names with non-ASCII characters. – nwellnhof Commented Mar 27, 2013 at 22:01
1 Answer
Reset to default 5If 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:
.*