0

I have an input which can accept all alphabetical characters [a-zA-Z] (lower case AND upper case). I have to attach a validator to this input to test if all characters have same case or not.

For example :

  • ABCD is valid
  • abcd is valid
  • AbCD is invalid
  • aBcd is invalid

Is there a way to do that with a regex ? Or should I use a custom function to do that ?

Hamza
  • 11

2 Answers2

1

Simple enough, all letters need to be either upper or lower case

(^[a-z]+$|^[A-Z]+$)
Dominik Matis
  • 2,086
  • 10
  • 15
0

You can use this function

let string = "hello"
const isOneCase = (string) => /^[a-z]*$|^[A-Z]*$/.test(string)

console.log(isOneCase(string))
Ahmed Gaafer
  • 1,603
  • 9
  • 26