-2

I am developing a sign-up page. In that, I want user to input password with the following format:

  • Must contains a series of letters and numbers or special characters
  • Order is not necessary
  • Reject if the string doesn't have a letter

Example:

Valid inputs:

abc123 
123abc 
abc!@#
!@#abc

Invalid inputs:

abc
123    
!@#    
123!@#

My work:

((([A-Z]|[a-z]|[0-9])*)(([0-9])|([[\]{}\\|;:'"_+=!@#$%$%^&*()-,<\.>/\?`~])))

It matches only abc123 and abc!@#.

Biffen
  • 6,249
  • 6
  • 28
  • 36
RagAnt
  • 1,064
  • 2
  • 17
  • 35
  • My Work ((([A-Z]|[a-z]|[0-9]))(([0-9])|([[]{}\|;:'"_+=!@#$%$%^&()-,<.>/\?`~]))) it matches only abc123,abc!@# – RagAnt Feb 14 '18 at 10:36
  • Is `[A-Za-z](?=[[\]{}\\|;:'"_+=!@#$%$%^&*()-,<\.>/\?\`~0-9])|(?<=[[\]{}\\|;:'"_+=!@#$%$%^&*()-,<\.>/\?\`~0-9])[A-Za-z]` fine ? – Leyff da Feb 14 '18 at 10:47
  • 2
    Although you can do this with a regular expression it is probably easier to just count how many letters, digits and specials you got. This would also allow more complicated rules (e.g at least 3 letters and 2 digits) – Henry Feb 14 '18 at 10:51
  • @Leyffda .. When it tested it in regexr.com , it shows "positive lookbehind not supported in this flavor of regex " – RagAnt Feb 14 '18 at 10:52
  • @anubhava .. Because the password must have both alphabets and numbers or special characters. Alphabets alone or numbers alone or special characters alone should be rejected – RagAnt Feb 14 '18 at 10:55
  • @LordCommander Please take a look at [Reference - Password Validation](https://stackoverflow.com/questions/48345922/reference-password-validation/48346033#48346033) – ctwheels Feb 14 '18 at 16:43

1 Answers1

2

Did you try the following ?

((([a-zA-Z]+)([0-9#]+))|(([0-9#]+)([a-zA-Z]+)))[a-zA-Z0-9#]*

# being replaced by allowed special characters.

It mandates an alphabet and a special character or a number followed by any number of allowed characters

Arun Gowda
  • 2,721
  • 5
  • 29
  • 50