Regular Expressions for City name

Manaysah picture Manaysah · Aug 1, 2012 · Viewed 42.7k times · Source

I need a regular Expression for Validating City textBox, the city textbox field accepts only Letters, spaces and dashes(-).

Answer

user2603432 picture user2603432 · Sep 5, 2014

This answer assumes that the letters which @Manaysah refers to also encompasses the use of diacritical marks. I've added the single quote ' since many names in Canada and France have it. I've also added the period (dot) since it's required for contracted names.

Building upon @UIDs answer I came up with,

^([a-zA-Z\u0080-\u024F]+(?:. |-| |'))*[a-zA-Z\u0080-\u024F]*$

The list of cities it accepts:

Toronto
St. Catharines
San Fransisco
Val-d'Or
Presqu'ile
Niagara on the Lake
Niagara-on-the-Lake
München
toronto
toRonTo
villes du Québec
Provence-Alpes-Côte d'Azur
Île-de-France
Kópavogur
Garðabær
Sauðárkrókur
Þorlákshöfn

And what it rejects:

A----B
------
*******
&&
()
//
\\

I didn't add in the use of brackets and other marks since it didn't fall within the scope of this question.

I've stayed away from \s for whitespace. Tabs and line feeds aren't part of a city name and shouldn't be used in my opinion.