user1729506
user1729506

Reputation: 975

How can I use javascript regex to validate a city name?

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.

Upvotes: 3

Views: 6043

Answers (1)

mayhewr
mayhewr

Reputation: 4021

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:

.*

Upvotes: 5

Related Questions