0

I have used devise gem in rails application. password format for validation I found is

PASSWORD_FORMAT_USED_CURRENTLY = /\A
  (?=.{10,})          # Must contain 10 or more characters
  (?=.*\d)           # Must contain a digit
  (?=.*[a-z])        # Must contain a lower case character
  (?=.*[A-Z])        # Must contain an upper case character
  (?=.*[[:^alnum:]]) # Must contain a symbol
/x

It works fine but I want two requirements to be fulfilled for my password 1) password must contain at least 6 letters ( it can be capital letters, small letters, digits or a combination of all ).

 PASSWORD_FORMAT = /\A
  (?=.{6,})          # Must contain 6 or more characters      
/x

2) the second criteria is that the password should not contain any special characters.

I don't know how to achieve this what I found is only that how can I make compulsory the presence of special characters but not vice versa.

Thanks in advance

anjali
  • 87
  • 1
  • 12
  • There's an answer to this here: https://stackoverflow.com/questions/6814780/regex-without-special-characters/6814901 – miles_b Nov 28 '18 at 00:47
  • this regex is ok but I am not able to implement it in my rails model file – anjali Nov 28 '18 at 06:21
  • 1
    Just wondering, what is the reason that one would need to prevent a user from entering a special character in a password? – webaholik Mar 22 '19 at 15:34

1 Answers1

0

The second regex itself will handle both the cases.

 PASSWORD_FORMAT = /\A
  (?=.{6,})          # Must contain 6 or more characters
  (?=.*\w)           # Must contain capital letters, small letters, digits or a combination of all. Must not contain any special characters.
/x
Dyaniyal Wilson
  • 1,012
  • 10
  • 14
  • Thanks for the reply. I tried this with password 123@12 and it accepts it. this accepts special characters. – anjali Nov 29 '18 at 01:42