0

I have a requirement that validates the user input.

  • only input a-z,A-Z, ', '-' and blanks.
  • a-z,A-Z should appear at least once.

In other words:

  • 'test user' --> True
  • 'test1user' --> False
  • ' test user' -' --> True
  • ' ' --> False
  • ' ' - ' --> False

I tried one way like ^[(A-Za-z)? '-]+$, but not work.

Any other solutions?

sherlockliu
  • 63
  • 1
  • 1
  • 6

1 Answers1

1

You may use this regex with a lookahead condition to ensure there must be at least one letter in input:

^(?=.*[a-zA-Z])[A-Za-z '-]+$

RegEx Demo

(?=.*[a-zA-Z]) is a positive lookahead condition that asserts presence of at least one letter in input text.

anubhava
  • 761,203
  • 64
  • 569
  • 643