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
  • 37,514
  • 71
  • 268
  • 523

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,596
  • 2
  • 32
  • 46
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
  • 19,681
  • 15
  • 61
  • 128