1

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);
  }
``
Icezua
  • 35
  • 1
  • 9
  • 1
    See [Reference - What does this regex mean?](/q/22937618/4642212) and the [regex tag wiki](/tags/regex/info) and use regex debuggers like [RegEx101](//regex101.com/). – Sebastian Simon Apr 03 '22 at 11:53

2 Answers2

2

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.

1

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);
}
Alireza Rezaee
  • 314
  • 3
  • 10