Icezua
Icezua

Reputation: 35

Input[type = 'text'] accept alphabets only instead of alphabets and whitespaces

I want to make my input text field accept alphabets only and space(at least one white space), not only alphabets, i use this expression but only alphabets works but if I put spaces between the text, doesnt work.

const isAlphabet = (input) => {
    const re = /^[a-zA-Z]+$/;
    return re.test(input);
  }
``

Upvotes: 1

Views: 728

Answers (2)

Bartłomiej Stasiak
Bartłomiej Stasiak

Reputation: 518

let change /^[a-zA-Z]+$/ for /^[a-z A-Z]+$/. i saw that someone used \s instead of , so i'm still sending my answer, since expression proposed by me is shorter, better performing (due to handling only space) plus u will gain additional knowledge, how to handle spaces in different way.

Upvotes: 2

Alireza Rezaee
Alireza Rezaee

Reputation: 312

Use \s (including space, tab, form feed, line feed, and other Unicode spaces)

const isAlphabet = (input) => {
    const re = /^[a-zA-Z\s]+$/;
    return re.test(input);
}

Upvotes: 1

Related Questions