0

A regular expression for password

  • It should have one number
  • One character compulsorily
  • Not case sensitive.
  • Special character is not mandatory.

xyz.match(/^(?:[0-9]+[a-z]|[a-z]+[0-9])[a-z0-9]*$/i)

In this if we give special character it is not accepting

asd12   //working
Asd123  //working
Asd123@ //not working 
Narendrasingh Sisodia
  • 21,247
  • 6
  • 47
  • 54
mage-kat
  • 21
  • 5

1 Answers1

1

You need 2 look-aheads anchored at the beginning instead of a non-capturing group:

/^(?=[^0-9]*[0-9])(?=[^a-z]*[a-z]).*$/i
  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 

The .* subpattern will allow any characters, but the look-aheads will demand at least 1 digit and 1 Latin letter.

Note that non-capturing groups still consume characters, and cannot be used to check for presence or absence of specific subpatterns in a string. Only zero-width assertions provide that mechanism.

See this regex demo

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563