12

I need write a regular expression for RegularExpressionValidator ASP.NET Web Controls.

The regular expression should ALLOW all alphabetic characters but not numbers or special characters (example: |!"£$%&/().

Any idea how to do it?

Brad Mace
  • 26,397
  • 17
  • 98
  • 144
GibboK
  • 68,054
  • 134
  • 405
  • 638

3 Answers3

20
^[A-Za-z]+$

validates a string of length 1 or greater, consisting only of ASCII letters.

^[^\W\d_]+$

does the same for international letters, too.

Explanation:

[^   # match any character that is NOT a
\W   # non-alphanumeric character (letters, digits, underscore)
\d   # digit
_    # or underscore
]    # end of character class

Effectively, you get \w minus (\d and _).

Or, you could use the fact that ASP.NET supports Unicode properties:

^\p{L}+$

validates a string of Unicode letters of length 1 or more.

Tim Pietzcker
  • 313,408
  • 56
  • 485
  • 544
  • Great answer; to complement it: `\p{Ll}` only matches lowercase characters, and `\p{Lu}` only uppercase ones. – mklement0 Nov 18 '15 at 03:28
  • @FrankHintsch: [Falsehoods programmers believe about names](http://www.kalzumeus.com/2010/06/17/falsehoods-programmers-believe-about-names/) :) – Tim Pietzcker Feb 12 '19 at 11:58
7

Including spaces:

"^[a-zA-Z ]*$"

Excluding Spaces:

"^[a-zA-Z]*$"

To make it non-optional, change the * to a +

cjk
  • 44,524
  • 9
  • 79
  • 111
3

You can use the regex:

^[a-zA-Z]+$

Explanation:

  • ^ : Start anchor
  • [..] : Char class
  • + : one or more repetations
  • $ : End anchor
codaddict
  • 429,241
  • 80
  • 483
  • 523