Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2034
  • Last Modified:

Key Ascii

Hi,

I have a password field where i display the password entered with a * and i append one more * to make it look like ** for every key stroke. I want to disable the space bar key from the password.. The code i have still gives one * for space bar..how do i supress it.


If KeyAscii = 32 Then
MsgBox "Space is not a valid key", vbOKOnly
txtPassword.SetFocus
Exit Sub

Else
txtPassword.Text = txtPassword.Text & "*"
txtPassword.SelStart = Len(txtPassword.Text)
End If

If KeyAscii = 8 Then
txtPassword.Text = Mid(txtPassword.Text, 1, Len(txtPassword.Text) - 2)
txtPassword.SelStart = Len(txtPassword.Text)
End If


Thanks
0
sramkris
Asked:
sramkris
  • 3
1 Solution
 
inthedarkCommented:
If KeyAscii = 32 Then
   MsgBox "Space is not a valid key", vbOKOnly
   txtPassword.SetFocus
    KeyAscii = 0
Exit Sub

0
 
inthedarkCommented:
Also suggest that you set the PasswordChar property of the textbox to "*"

In this way you only need the following code (EXAMPLE FOR TEXT1):

Private Sub Text1_KeyPress(KeyAscii As Integer)

Static ValidKeys As String
Static Done As Boolean

If Not Done Then
    ' ADD YOU VALID KEYS HERE:
    ValidKeys = "0123456789abcdefghijklmnopqrstuvwxyzABCDEGHIJLKMNOPQRSTUVWXYZ`¬!""£$%^&*(){}_+=[]/\?><,.;:'"
End If
If InStr(ValidKeys, Chr(KeyAscii)) = 0 Then
    KeyAscii = 0
    MsgBox "Invalid Password Key", vbExclamation, "Error"
    Text1.SetFocus
End If

End Sub

Hope this helps....inthedark
0
 
KarcOriginCommented:
Hi,
Try this -


Private Sub Text1_KeyPress(KeyAscii As Integer)
    If KeyAscii = 32 Then KeyAscii = 0
End Sub
0
 
inthedarkCommented:
Woops

If Not Done Then
   Done = True ' I forgot this
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now