Search code examples
angularjsregexng-pattern

ng-pattern for exclusion group of numbers


I have a list of exclusion numbers.

for example (400276|400615|402914|404625)

the pattern should not let me enter into the input any of these numbers as the first 6 digits example

400276123 .BAD. because the value init with a number to exclude

400277123 .OK

I try something like that

"^[^] (400|405)"

but is not working

how can I use a pattern for exclude this first 6 digits


Solution

  • Your pattern - ^[^] (400|405) - matches the start of the string, then any char, a space, and either 400 or 405.

    What you need is a negative lookahead:

    /^(?!400276|400615|402914|404625)/
      ^^^                           ^
    

    It will fail the match of a string that starts with these values.

    See the regex demo.