devxlogo

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?

Question:
How can I let a user enter a password and hide the charactersentered?

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

If the keycode parameter is “KEYCODE_CHAR”, which is an internalPower Objects constant which is equal to 0, you know that analphanumeric character or punctuation mark was entered. The codebelow tests to see if the keycode is KEYCODE_CHAR. If it is, it addsthe value of the keychar variable to a user-defined variable calledudpPassword. The code then creates a string which consists of astring of “*” characters which is as long as the current value of theudpPassword property and sets the value of the field to the string.Finally, you return True from the OnKey() method to suppress thenormal processing for the keystroke.

You will also want to test to see if the user has entered theeither arrow keys to navigate through the value entered in the fieldor the DELETE keystroke. Since you are saving the keystrokes enteredin the udpPassword sequentially, your code would be significantlymore complicated if you let your users navigate around in thecharacters, since you would have to determine where the user was inthe string of asterisks and add or delete the keystroke at thecorrect position in the user-defined property. Also, when you usethe code described above to replace characters with an asterisk, theedit 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 userthat they can’t use arrow keys or delete keys in entering a passwordand then clear the object to avoid confusion.

      DIM nPointer As Integer      DIM nCounter As Integer      DIM sPassword As String      IF KEYCODE = KEYCODE_CHAR THEN              udpPassword = udpPassword & keychar              nPointer = 1              nLength = Len(udpPassword)              DO UNTIL nPointer > nLength                      sPassword = sPassword & "*                      nPointer = nPointer + 1              LOOP              Self.Value = sPassword              OnKey = True      ELSEIF KEYCODE = KEYCODE_BACKSPACE OR keycode = KEYCODE_LEFT &              OR KEYCODE = KEYCODE_RIGHT THEN      MSGBOX ("You must enter your password directly without " & &               "deletions or using the arrow keys.",               16,               "Password Entry")                      OnKey = true                              Self.Value = ""                              udpPassword = ""      END IF        

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

      Self.Value = ""      udpPassword = ""        

Since passwords are normally only a few characters, this doesn’tseem like such a big imposition on the user, especially to maintainthe integrity of your security system.

See also  Why ChatGPT Is So Important Today
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist