isAlpha
Checks if a string contains only alphabetic characters (A–Z, a–z).
1/**
2 * Checks if a string contains only alphabetic characters (A–Z, a–z).
3 *
4 * @param str - The string to validate.
5 * @returns True if the string contains only letters, false otherwise.
6 */
7export function isAlpha(str: string): boolean {
8 return /^[A-Za-z]+$/.test(str);
9}
Simple and Efficient Validation
Uses a concise regular expression to quickly determine if a string contains only alphabetic characters.
Language-Agnostic for Basic Latin Alphabet
Focuses on ASCII A–Z and a–z, making it reliable for English-based validations.
Zero Dependencies
No need for external libraries or Unicode tables — lightweight and self-contained.
Tests | Examples
1test('isAlpha - valid alphabetic strings', () => {
2 expect(isAlpha('Hello')).toBe(true);
3 expect(isAlpha('world')).toBe(true);
4 expect(isAlpha('ABC')).toBe(true);
5});
6
7test('isAlpha - invalid strings with numbers', () => {
8 expect(isAlpha('Hello123')).toBe(false);
9 expect(isAlpha('123')).toBe(false);
10});
11
12test('isAlpha - invalid strings with symbols or spaces', () => {
13 expect(isAlpha('Hello!')).toBe(false);
14 expect(isAlpha('')).toBe(false);
15 expect(isAlpha('Hello World')).toBe(false);
16});
Common Use Cases
First/Last Name Validation
Ensure user input fields for names do not include numbers or special characters.
Sanitizing Form Data
Validate input fields where only letters are expected, such as country codes or tags.
Token or Identifier Checks
Confirm that specific identifiers or codes follow an alphabetic-only format.
Basic Word Filtering
Use in simple content moderation or spam detection to check for alphabet-only strings.