Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression to accept only characters (a-z) in a textbox

Tags:

regex

What is a regular expression that accepts only characters ranging from a to z?

like image 301
Muthukumar Avatar asked Nov 22 '10 09:11

Muthukumar


People also ask

How do I allow only special characters in regex?

You can use this regex /^[ A-Za-z0-9_@./#&+-]*$/.

What is the use of * \d and \w in regular expressions?

Regex uses backslash ( \ ) for two purposes: for metacharacters such as \d (digit), \D (non-digit), \s (space), \S (non-space), \w (word), \W (non-word). to escape special regex characters, e.g., \. for . , \+ for + , \* for * , \? for ? .

What does '$' mean in regex?

$ means "Match the end of the string" (the position after the last character in the string). Both are called anchors and ensure that the entire string is matched instead of just a substring.


4 Answers

The pattern itself would be [a-z] for single character and ^[a-z]+$ for entire line. If you want to allow uppercase as well, make it [a-zA-Z] or ^[a-zA-Z]+$

like image 175
Dyppl Avatar answered Sep 28 '22 16:09

Dyppl


Try this to allow both lower and uppercase letters in A-Z:

/^[a-zA-Z]+$/

Remember that not all countries use only the letters A-Z in their alphabet. Whether that is an issue or not for you depends on your needs. You may also want to consider if you wish to allow whitespace (\s).

like image 24
Mark Byers Avatar answered Sep 28 '22 17:09

Mark Byers


Allowing only character and space in between words :

^[a-zA-Z_ ]*$

Regular Expression Library

like image 27
GowriPrakash Avatar answered Sep 28 '22 17:09

GowriPrakash


^[A-Za-z]+$ To understand how to use it in a function to validate text, see this example

like image 29
CodingSolutions Avatar answered Sep 28 '22 18:09

CodingSolutions