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.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS