2

In my react application I have to show an error if an user name already exists.

const regex = '(?:' + availableNames.join('|') + ')';

<div className="mdl-textfield mdl-js-textfield mdl-textfield--floating-label">

    <input className="mdl-textfield__input" pattern={regex} autoFocus/>

    <label className="mdl-textfield__label" htmlFor="inp-1">User name</label>
    <span className="mdl-textfield__error" >Name already exists</span>
</div>

I have started with the following expression (?:alex|max|sam) and I know that this allows the user only to input alex, max or sam.

But I can't manage to negate the expression. I would be grateful for any help

alex
  • 8,904
  • 6
  • 49
  • 75

1 Answers1

1

You try something like this with negative lookahead

^((?!\b(alex|max|sam)\b).)*$
  • ^ - Start of string
  • (?!\b(alex|max|sam)\b) - Do not match any word of these words alex, sam, max
  • $ - End of string

Demo

Code Maniac
  • 37,143
  • 5
  • 39
  • 60