0

I have this regex to match username in my system: /[a-zA-Z]+([_-]?[0-9a-zA-Z]+)*$/. It starts with letters and allows numbers, _ and -. But I dont want the username includes some words like admin, facebook, official etc. How can I add these words to the regex? I want to use one regex to manage all the constraints.

Joey Yi Zhao
  • 30,508
  • 51
  • 183
  • 377

2 Answers2

1

Here's a possibility:

^((?!admin|facebook|official)[a-zA-Z])((?!admin|facebook|official|[-_]{2})[-_0-9a-zA-Z])*$

Demo

Edit: updated to make it more efficient.

Jeto
  • 14,091
  • 2
  • 30
  • 43
1
/^(?=((?!(admin|facebook|official)).)+$)[a-z]+[_-]?[0-9a-z]+$/i

Check it:

input { width: 100%; box-sizing: border-box; outline: none; }
:valid { border: 1px solid green; }
:invalid { border: 1px solid red; }
<input pattern="^(?=((?!(admin|facebook|official)).)+$)[a-zA-Z]+[_-]?[0-9a-zA-Z]+$" autofocus>
Qwertiy
  • 17,208
  • 13
  • 50
  • 117