3

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.

user1729506
  • 975
  • 4
  • 15
  • 28

1 Answers1

5

If the comments 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:

.*

Community
  • 1
  • 1
mayhewr
  • 4,003
  • 1
  • 18
  • 15
  • +1, although I would recommend `.+`. I think we can confidently say that there are no zero-width city names. – JDB Mar 27 '13 at 22:54
  • @Cyborgx37 Hah. I was gonna go with `[^\n]+`, since surely there are no 2-line cities. What if I live in the middle of nowhere in no city? Should you reject me for not living in a city? Certainly not. I think `.*` makes the point more clearly. – mayhewr Mar 27 '13 at 22:58