I'm using the
^(?=^.{14,30}$)(?=^.[\d]{3}$)(?=^.[a-z]{4}$)(?=^.[A-Z]{5}$)(?=^.[!*&#$%^]{2}).$
regular expression in order to validate the following as minimum requirements:
But Unfortunately This validation pattern doesn't validate my string.
I've look for regex
pattern validation and gives me that the pattern is OK
Can someone give me a support on this?
Note that (?=^.[az]{4}$)
requires a match of a 5 char string where the first one csn be any char and then there must be 4 lowercase letters. In the end, after ^
and lookaheads, there is .
before $
, so actually, the whole regex can match a single char string.
You may use
^(?=(?:\D*\d){3})(?=(?:[^a-z]*[a-z]){4})(?=(?:[^A-Z]*[A-Z]){5})(?=(?:[^!*&#$%^]*[!*&#$%^]){2}).{8,30}$
See the regex demo . A regulex graph :
Details
^
- start of string (?=(?:\\D*\\d){3})
- three occurrences of any 0+ non-digits followed with a digit (?=(?:[^az]*[az]){4})
- four occurrences of any 0+ chars other than a lowercase ASCII letters followed with a lowercase ASCII letter (?=(?:[^AZ]*[AZ]){5})
- five occurrences of any 0+ chars other than an uppercase ASCII letters followed with an uppercase ASCII letter (?=(?:[^!*&#$%^]*[!*&#$%^]){2})
- two occurrences of any 0+ chars other than some specific special chars (defined in the character class) followed with a char from the sepcified !*&#$%^
set .{8,30}
- any 8 to 30 chars $
- end of string. ^(?=.{14,30}$)(?=(?:.*[A-Z]){5})(?=(?:.*[a-z]){4})(?=(?:.*\d){3})(?=(?:.*[!*&#$%^]){2}).*$
└─────┬─────┘└───────┬────────┘└───────┬────────┘└──────┬──────┘└─────────┬──────────┘
│ │ │ │ │
│ │ │ │ 2 special characters
│ │ │ │
│ │ │ 3 digits
│ │ │
│ │ 4 lowercase letters
│ │
│ 5 uppercase letters
│
string is 14-30 characters long
^(?=(?:.*[A-Z]){5})(?=(?:.*[a-z]){4})(?=(?:.*\d){3})(?=(?:.*[!*&#$%^]){2}).(14,30)$
└───────┬────────┘└───────┬────────┘└──────┬──────┘└─────────┬──────────┘
│ │ │ │
│ │ │ 2 special characters
│ │ │
│ │ 3 digits
│ │
│ 4 lowercase letters
│
5 uppercase letters
Instead of a regex, which may be difficult to maintain, how about some simple code that is easy to read and modify:
Private Function IsStringValid(s As String) As Boolean
If (s.Length > 30) OrElse (s.Length < 14) Then
Return False
End If
Dim lower, upper, digit, special As Integer
For Each c As Char In s
Select Case True
Case Char.IsLower(c)
lower += 1
Case Char.IsUpper(c)
upper += 1
Case Char.IsDigit(c)
digit += 1
Case Char.IsSymbol(c) OrElse Char.IsPunctuation(c)
special += 1
End Select
Next
Return (lower >= 4) AndAlso (upper >= 5) AndAlso (digit >= 3) AndAlso (special >= 2)
End Function
Based on your requirements, the minimum length has to be 14.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.