最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - How can I use javascript regex to validate a city name? - Stack Overflow

programmeradmin0浏览0评论

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

.*

发布评论

评论列表(0)

  1. 暂无评论