Hi,
I want to have a password which consists capital letters, small letters, numbers and special characters. How can I make it?
Thanks for replying
Printable View
Hi,
I want to have a password which consists capital letters, small letters, numbers and special characters. How can I make it?
Thanks for replying
On the assumption it has to be 8 characters or more in length and must have at least: one Capital Letter, one Lower Case Letter, one Nmber and one Special Character:
Code:Private Sub cmdChekPWD_Click()
Dim bytFlag As Byte
Dim intI As Integer
Dim strC As String
If Len(txtPassword.Text) >= 8 Then
For intI = 1 To Len(txtPassword.Text)
strC = Mid$(txtPassword.Text, intI, 1)
If strC >= "!" And strC <= "\" Then
bytFlag = bytFlag Or &H8
End If
If strC >= "0" And strC <= "9" Then
bytFlag = bytFlag Or &H4
End If
If strC >= ":" And strC <= "@" Then
bytFlag = bytFlag Or &H8
End If
If strC >= "A" And strC <= "Z" Then
bytFlag = bytFlag Or &H2
End If
If strC >= "a" And strC <= "z" Then
bytFlag = bytFlag Or &H1
End If
Next intI
End If
If bytFlag <> &HF Then
MsgBox "Invalid Password"
End If
End Sub
"must have at least: one Capital Letter, one Lower Case Letter, one Number and one Special Character"
Unless I misunderstand the requirements it looks like Axxxxx111 passes the test but does not have any special characters.
Try this:
Code:Option Explicit
Private Sub Command1_Click()
If Not CheckPassword("abcdefg123") Then
MsgBox "Invalid password - it must contain at least 1 capital letter, 1 lower and 1 numeric character.", _
vbExclamation, "Invalid Password"
Exit Sub
Else
'proceeed with normal processing...
Debug.Print "password is ok..."
End If
End Sub
Private Function CheckPassword(sPwd As String) As Boolean
Dim i As Integer
Dim bCapital As Boolean
Dim bLower As Boolean
Dim bNumberic As Boolean
Dim iAscii As Integer
For i = 1 To Len(sPwd)
iAscii = Asc(VBA.Mid$(sPwd, i, 1))
If Not bCapital Then
bCapital = CBool(iAscii >= 65 And iAscii <= 90)
End If
If Not bLower Then
bLower = CBool(iAscii >= 97 And iAscii <= 122)
End If
bNumberic = IsNumeric(VBA.Mid$(sPwd, i, 1))
If bCapital And bLower And bNumberic Then Exit For
Next i
CheckPassword = (bCapital And bLower And bNumberic)
End Function
Regular expression can be used here also.Code:Private Function ValidatePassword(ByVal sPass) As Boolean
Dim regEx
Set regEx = CreateObject("vbscript.regexp")
'At least 8 characters
'At least 1 number
'At least 1 lowercase letter
'At least 1 uppercase letter
'At least 1 special character. Special charaters include !@#$%^&+=
regEx.Pattern = "^.*(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%^&+=]).*$"
ValidatePassword = regEx.Test(sPass)
Set regEx = Nothing
End Function
do you want to make one ( function generator(byval size as integer, byval specials as string)as string)?
or just test one (function Test(by val pwd as string) as bool )?
the use of special characters can be a problem for systems so..
can you provide an allowable set for the system/user to use?
oh yes its not just the girls who think size matters... so...
how many digits?
here to talk
nice one MarkT
I was going to do something similar
but can you explain the regular expression expression for the benefit of the forum
its a little too esoteric for some of them
here to talk
Firstly, thanks for replies..
Actually I wanted the user has to determine a password which consists at least; one capital letter, one small letter, one number and one special character as you said..
Doogle, it works.. Could you explain its idea?
RhinoBull, it always says "Invalid password - it must contain at least 1 capital letter, 1 lower and 1 numeric character."
MarkT, I didn't understand what you meant..
What didn't you understand? The usage would beCode:Private Function ValidatePassword(ByVal sPass) As Boolean
Dim regEx
Set regEx = CreateObject("vbscript.regexp")
'At least 8 characters
'At least 1 number
'At least 1 lowercase letter
'At least 1 uppercase letter
'At least 1 special character. Special charaters include !@#$%^&+=
regEx.Pattern = "^.*(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%^&+=]).*$"
ValidatePassword = regEx.Test(sPass)
Set regEx = Nothing
End Function
Private Sub Command1_Click()
MsgBox "Valid = " & ValidatePassword(Text1.Text)
End Sub
"Doogle, it works.. Could you explain its idea?"
Did you try Axxxxx111
Yup, there's an error in my code.
should beCode:If strC >= "!" And strC <= "\" Then
Apart from that, I think it solves OP's original problem (although it's probably not the best solution)Code:If strC >= "!" And strC <= "/" Then
BTW It works by checking each character in txtPassword, if it's a 'special character' between '!' and '/' it sets Bit 3 of bytFlag, if it's between '0' and '9' it sets Bit 2 of bytFlag, if it's between ':' and '@' it sets Bit 3 of bytFlag, if it's between 'A' and 'Z' it sets Bit 1 of bytFlag, if it's between 'a' and 'z' it sets bit 0 of bytFlag.
So after all the characters have been examined, if there was at least one of each (Capital, Lower Case, Number, Special Character) bytFlag will equal &h0F.
(Bit0 = 1, Bit1 = 2, Bit2 = 4, Bit3 = 8, add them all up and it's 15 = &hF)
thats what I like to see most of you telling what it does and not just telling what it is
If this continues the newbies might learn a thing or two
long may it continue...
but what does, for the benefit of the op and other forum users
the
meanCode:regEx.Pattern = "^.*(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%^&+=]).*$"
the structure and how to read it would help everyone to get theri heads around regex
for many it is more than alien territory
here to ... (well i am just here)
Regular expressions aren't something, at least to me, you can just easily explain. You need to know what kind of animal it is and the basic concepts. Look here:
http://www.regular-expressions.info/
Yea, what TysonLPrice said.
Here is another link that will probably confuse you more as to what the expression is doing. http://www.regular-expressions.info/reference.html
Basically you can break it apart for each of the checks you need
The check for least at 8 characters
The check for at least 1 number
The check for at least 1 lowercase letter
The check for at least 1 uppercase letter
The check for at least 1 special character. Special charaters include !@#$%^&+=
^.*(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%^&+=]).*$
To be honest most of the time if I need a complex expression I will just google for it.
the reference to the regex expression is a good idea
its the sheer alien-ness of them that I think the newbies will find hard if not horrifying
its all well and good to say "rolled up bits of newspaper 1 inch wide will stop the elephants from coming"
and to be "proved right" because they havent arrived yet!
its another to understand why the process works and how to use it fruitfully in the future against the rhinos
its the how and why they need ( newbies )
the answers alone are not going to broaden their understanding
there I go ranting again... I'd better off and check my sugars
here to talk
Thanks for your replies, I think it's gonna work anymore