I'm looking to put together a basic validation for a U.S. city. The criteria would be that alpha characters, spaces, periods, hyphens, and apostrophes are allowed, with nothing else. So the following would be allowed:

Saint Louis
St. Louis
Washington D.C.
O'Hara (do any US cities exist with apostrophes?)
Raleigh-Durham

I've started this:

$regex = "^[[:alpha:][:space:]/.-]*$";

However, it still allows for anything that is allowed to trail the subject, ie. "St. Louis." is allowed.

What's the best way to prevent this?

Otherwise, is there a better way to handle a valid city name?

    kilbey1 wrote:

    Otherwise, is there a better way to handle a valid city name?

    Keep a gazetteer handy? No, really; the only thing about a city name is that it is a proper noun. Whether it's valid or not depends on whether it exists or not. Beyond that it's almost fair game (for example, the U.S. Government renders the capital's name as "Washington, DC" - no dots, but a comma); and isn't there a town called "9" somewhere in the US?

      Write a Reply...