'Regular expression to accept only characters (a-z) in a textbox
What is a regular expression that accepts only characters ranging from a to z?
Solution 1:[1]
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).
Solution 2:[2]
Solution 3:[3]
^[A-Za-z]+$ To understand how to use it in a function to validate text, see this example
Solution 4:[4]
Solution 5:[5]
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...
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...
Solution 6:[6]
Just for people using bash shell, instead of "+" use "*"
"^[a-zA-Z]*$"
Solution 7:[7]
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)) {
}
Solution 8:[8]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Mark Byers |
| Solution 2 | |
| Solution 3 | CodingSolutions |
| Solution 4 | Peter Mortensen |
| Solution 5 | Lucky |
| Solution 6 | BatBold |
| Solution 7 | |
| Solution 8 |
