Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: SQL
Expertise: Beginner
Mar 21, 1997



Building the Right Environment to Support AI, Machine Learning and Deep Learning

How can I let a user enter a password and hide the characters entered?

How can I let a user enter a password and hide the characters entered?

You can let the user enter characters into a field and replace the letters entered by using the OnKey() method of the object. The OnKey() method is triggered whenever the user enters a keystroke into the field. The OnKey() method gives you three parameters: the keychar, the keycode, and the shift parameter. The keychar gives you the character that was entered, the keycode tells you what type of keystroke was entered, and the shift tells you whether the shift and/or control keys were also pressed. You will only care about the first two parameters.

If the keycode parameter is "KEYCODE_CHAR", which is an internal Power Objects constant which is equal to 0, you know that an alphanumeric character or punctuation mark was entered. The code below tests to see if the keycode is KEYCODE_CHAR. If it is, it adds the value of the keychar variable to a user-defined variable called udpPassword. The code then creates a string which consists of a string of "*" characters which is as long as the current value of the udpPassword property and sets the value of the field to the string. Finally, you return True from the OnKey() method to suppress the normal processing for the keystroke.

You will also want to test to see if the user has entered the either arrow keys to navigate through the value entered in the field or the DELETE keystroke. Since you are saving the keystrokes entered in the udpPassword sequentially, your code would be significantly more complicated if you let your users navigate around in the characters, since you would have to determine where the user was in the string of asterisks and add or delete the keystroke at the correct position in the user-defined property. Also, when you use the code described above to replace characters with an asterisk, the edit cursor for the field is always at the beginning of the field. To avoid complex code, you can just add the code that tells the user that they can't use arrow keys or delete keys in entering a password and then clear the object to avoid confusion.

      DIM nPointer As Integer

      DIM nCounter As Integer

      DIM sPassword As String


              udpPassword = udpPassword & keychar

              nPointer = 1

              nLength = Len(udpPassword)

              DO UNTIL nPointer > nLength

                      sPassword = sPassword & "*

                      nPointer = nPointer + 1


              Self.Value = sPassword

              OnKey = True



      MSGBOX ("You must enter your password directly without " & &

               "deletions or using the arrow keys.",


               "Password Entry")

                      OnKey = true

                              Self.Value = ""

                              udpPassword = ""

      END IF


You will have the same problem if a user leaves the password field and then re-enters the field. Let's just stop this foolishness in its tracks by clearing out the udpPassword property and the value of the object itself whenever a user enters the object. To achieve this, add the following code to the FocusEntering() method for the object.

      Self.Value = ""

      udpPassword = ""


Since passwords are normally only a few characters, this doesn't seem like such a big imposition on the user, especially to maintain the integrity of your security system.

DevX Pro
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date