0

I want to make that input take at least one lowercase, one uppercase, and only 2 numaric 0-9 and at least 8 characters (not more than 2 numaric) I try this pattern

(?=(?:.*\d){2})(?=.*[A-Z])(?=.*[a-z]).{8,}

Means, 4 characters (2 number + 1 lowercase + 1 uppercase letter) are must need and other characters can be anything .

I also tries this pattern

(?=(?:.*\d){2})(?=.*[A-Z])(?=.*[a-z])[^0-9]{8,}

and this

(?=(?:.*\d){2})(?=.*[A-z])(?=.*[a-z])[A-z!"#$%&'()*+,-.\/:;<=>?@[\]^_{|}~]{8,}.But anything could not work as my needed .

I want Like...

Abc4k9h2L ->FALSE

Abc4k9hL+ ->TRUE 

Abc4l@s$kl4 ->TRUE 

How to do that?

georg
  • 195,833
  • 46
  • 263
  • 351
  • 2
    add some sample input and output. is 2 numeric digits are must ? or it can have two, one or zero ? – Code Maniac Sep 15 '19 at 08:46
  • Yes, 2 numeric digits are must. I want Like... Abc4k9h2L ->FALSE Abc4k9hL+ ->TRUE Abc4l@s$kl4 ->TRUE – RAKTIM BANERJEE Sep 15 '19 at 08:49
  • I'd do it like [`^(?=.{8})(?=.*?[a-z])(?=.*?[A-Z])\D*\d\D*\d\D*$`](https://regex101.com/r/cptuN9/1) which is very similar to CodeManiacs answer that i voted for already. – bobble bubble Sep 15 '19 at 09:27

1 Answers1

4

You can modify your regex a bit

^(?=^[^\d]*\d[^\d]*\d[^\d]*$)(?=.*[a-z])(?=.*[A-Z]).{8,}$

enter image description here

Regex demo

const regex = /^(?=^[^\d]*\d[^\d]*\d[^\d]*$)(?=.*[a-z])(?=.*[A-Z]).{8,}$/;
const arr = ['123456789', 'abcdefghi', 'ABCDEFGHI', 'abcdABC12', 'abcdABCD1', '.12nabdh#A','Abc4k9h2L','Abc4k9hL+','Abc4l@s$kl4']

arr.forEach(str => {
  console.log(str, regex.test(str))
})

If you need a simplified manner to the same you can do something like this, check individual rule separately

const regex = /^(?=^[^\d]*\d[^\d]*\d[^\d]*$)(?=.*[a-z])(?=.*[A-Z]).{8,}$/;
const arr = ['123456789', 'abcdefghi', 'ABCDEFGHI', 'abcdABC12', 'abcdABCD1', '.12nabdh#A', 'Abc4k9h2L', 'Abc4k9hL+', 'Abc4l@s$kl4']

arr.forEach(str => {
  let length = str.length > 7
  let checkLowerCase = /[a-z]/.test(str)
  let checkUpperCase = /[A-Z]/.test(str)
  let digits = str.match(/\d/g)
  let twoDigits = digits && digits.length === 2
  let final = Boolean(length && checkLowerCase && checkUpperCase && twoDigits)

  console.log(str, final)
})
Code Maniac
  • 33,907
  • 4
  • 28
  • 50
  • @Code_maniac Sir, can you describe the first group where you accept only 2 digits. I can not understand. – RAKTIM BANERJEE Sep 15 '19 at 13:36
  • @RAKTIMBANERJEE have you seen the image, it has very good explanation of what first group is doing, go through, let me know if you still feel anything which is not quite clear to you, and no sir please `¯\_(ツ)_/¯` – Code Maniac Sep 15 '19 at 13:39
  • Yes, I watched the image. But what is it mean by accepting none of the digits then accepting a digit? How the JS engine work in this group? – RAKTIM BANERJEE Sep 15 '19 at 13:51
  • @RAKTIMBANERJEE regex engine parse string from start as we have `^` so than it check for zero or more non digit characters which then should be followed by digit character, it repeats it the same pattern twice as we have place it twice, and then check for non digit characters upto end of string, [`reference`](https://stackoverflow.com/questions/22937618/reference-what-does-this-regex-mean) – Code Maniac Sep 15 '19 at 13:59
  • Yes, Now I can understand. Thank a lot – RAKTIM BANERJEE Sep 15 '19 at 14:10