10

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Muthukumar
  • 8,679
  • 17
  • 61
  • 86
  • Sorry Actaully i have a text box,which should accept only strings with characters ranging from a to z – Muthukumar Nov 22 '10 at 09:58
  • Possible duplicate of [Regular Expression to match only alphabetic characters](https://stackoverflow.com/questions/6067592/regular-expression-to-match-only-alphabetic-characters) – bummi Apr 11 '19 at 19:14

9 Answers9

25

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]+$

Robert Koritnik
  • 103,639
  • 52
  • 277
  • 404
Dyppl
  • 12,161
  • 9
  • 47
  • 68
  • 1
    why have you used `+` sign before `$` sign in the last syntax.? – Zain Shaikh Nov 22 '10 at 09:57
  • Sorry, I missed the `+` in the first one. The `+` sign means "1 or more" – Dyppl Nov 22 '10 at 10:44
  • this actually wont stop users to enter numbers or symbols in text box.this makes the input box invalid. is there anyway to prevent users not to enter numbers & symbols..etc except strrings? – rgk Dec 04 '16 at 18:01
  • In c#, it doesn't work new Regex(@"^[a-zA-Z]+$").Match("This is a long string").Value what is wrong with it. – Anirudha Gupta Mar 27 '18 at 05:06
13

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).

Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
4

Allowing only character and space in between words :

^[a-zA-Z_ ]*$

Regular Expression Library

3

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

1

None of the answers exclude special characters... Here is regex to ONLY allow letters, lowercase and uppercase.

/^[_A-zA-Z]*((-|\s)*[_A-zA-Z])*$/g

And as for different languages, you can use this function to convert letters to english letters before the check, just replace returnString.replace() with letters you need.

export function convertString(phrase: string) {
    var maxLength = 100;

    var returnString = phrase.toLowerCase();
    //Convert Characters
    returnString = returnString.replace("ą", "a");
    returnString = returnString.replace("č", "c");
    returnString = returnString.replace("ę", "e");
    returnString = returnString.replace("ė", "e");
    returnString = returnString.replace("į", "i");
    returnString = returnString.replace("š", "s");
    returnString = returnString.replace("ų", "u");
    returnString = returnString.replace("ū", "u");
    returnString = returnString.replace("ž", "z");

    // if there are other invalid chars, convert them into blank spaces
    returnString = returnString.replace(/[^a-z0-9\s-]/g, "");
    // convert multiple spaces and hyphens into one space
    returnString = returnString.replace(/[\s-]+/g, " ");
    // trims current string
    returnString = returnString.replace(/^\s+|\s+$/g, "");
    // cuts string (if too long)
    if (returnString.length > maxLength) returnString = returnString.substring(0, maxLength);
    // add hyphens
    returnString = returnString.replace(/\s/g, "-");

    return returnString;
}

Usage:

const firstName = convertString(values.firstName);

            if (!firstName.match(allowLettersOnly)) {
            }
TSlegaitis
  • 1,231
  • 15
  • 29
  • *"None of the answers exclude special characters..."*: yours too: `/^[_A-zA-Z]*((-|\s)*[_A-zA-Z])*$/.test(']\[[]')` returns `true`. Also, `"ąąą".replace("ą", "a");` returns `"aąą"` (when the first parameter of `String.prototype.replace` is a string, only one occurrence is replaced, not all). – Casimir et Hippolyte Oct 18 '19 at 11:23
0

Try to use this plugin for masking input...you can also check out the demo and use this plugin if this is what you may want...

Masked Input Plugin

As you can see in the demonstration that you can use both alphatbets and numbers in a combination for complex textbox validations where an user might want to type not only alphatbets(azAZ) but also with numbers too(ie. alphanumberics)...specific validations like accepting only numbers in particular format(eg.phone numbers) can be done...that is the case when you can use this plugin for different circumstances..

hope this helps...

Lucky
  • 16,787
  • 19
  • 117
  • 151
0

Use

^[a-zA-Z]$

and browse for more at Expressions in category: Strings.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Singleton
  • 3,701
  • 3
  • 24
  • 37
0

Just for people using bash shell, instead of "+" use "*"

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

BatBold
  • 21
  • 1
  • 5
0

Match any word that contains any character in this group: [a-zA-Z0-9_]

/^[\w]+$/

Eg.

Match: abcz, AbcZ, abc_1, AbC_1

No match: abc z abc-z Abc-z AbC-9 aBc,12

C. Draghici
  • 157
  • 5