1

I want a regex that check "Minimum 8 characters at least one number and one special character, maximum 32 characters." , My regex is :

^(?=.*\d)(?=.*[a-zA-Z])[0-9a-zA-Z]{8,16}$
samsonasik
  • 440
  • 1
  • 6
  • 11

2 Answers2

4

I test this regex can work satisfied

/(?=^.{8,32}$)(?=(?:.*?\d){1})(?=.*[a-z])(?=(?:.*?[!@#$%*()_+^&}{:;?.]){1})(?!.*\s)[0-9a-zA-Z!@#$%*()_+^&]*$/

you can watch the live demo : http://jsfiddle.net/tuxrM/

var re = new RegExp(/(?=^.{8,32}$)(?=(?:.*?\d){1})(?=.*[a-z])(?=(?:.*?[!@#$%*()_+^&}{:;?.]){1})(?!.*\s)[0-9a-zA-Z!@#$%*()_+^&]*$/);

var test1 = '1234567',      //  less than 8 characters
    test2 = 'aaaaaaaa',     //  no  number & no special character
    test3 = 'aaaaaaa1',     //  no special character
    test4 = 'aaaAaa#1',     //  satisfied
    test5 = 'abcdefghigklmnopqrstuvwxyz1234567890332123';  //  more than 32 characters

re.test(test1);     //  FALSE
re.test(test2);     //  FALSE
re.test(test3);     //  FALSE
re.test(test4);     //  TRUE
re.test(test5);     //  FALSE
momo
  • 185
  • 1
  • 6
2

This is actually best solved by 3 different regular expressions.

/[0-9]/ //Check for at least one number.
/(SPECIAL CHARACTER)/ //Check for at least one special character. Please define "special" character.

And then

str.length >= 8 && str.length <= 32

If this is a password, do not limit your users in password length.

Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308