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

Time:04-04

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);
  }
``

CodePudding user response:

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.

CodePudding user response:

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);
}
  • Related