Password field.

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
zaxos
Posts: 222
Joined: Thu May 23, 2013 11:15 pm

Password field.

Post by zaxos » Wed Dec 17, 2014 4:08 am

Hello ppl, well i couldent find any good solution about passwords in fields so i made mine, i'l share it in case anyone else needs it.
- No need for extra images
- No need for extra fonts
works out of the box with livecode even selected text can be erased properly, just put the code inside a field and its ready.

Code: Select all

local tChar,tNumber,buffer
on textChanged
   set the lockText of me to false
   repeat for each char theChar in me
      if theChar is "*" then
         -- do nothing
      else
         add 1 to tNumber
         put theChar after tChar
      end if
   end repeat
   replace return with "" in tChar
   if buffer is an integer then
      if buffer > tNumber then
         delete last char of theChar
      end if
   end if
   put tNumber into buffer
   repeat for each char theChar in tChar
      put "*" after theAsterisk
   end repeat
   put theAsterisk into me
   put "X" after me
   delete last char of me
   --put tChar -- Uncoment this to see the text inside the field
end textChanged

on backSpaceKey
   if the selectedText <> empty then
      put the charIndex of the selectedText into theCharIndex
      repeat for each char theChar in the selectedText
         add 1 to theChars
      end repeat
      repeat for theChars
         delete char theCharIndex in tChar
      end repeat
   else
      delete last char in tChar
   end if
   textChanged
end backSpaceKey

on deleteKey
   delete first char in tChar
   textChanged
end deleteKey

on rawKeyDown theKey
   if theKey is not 65288 then
      if the selectedText <> empty then
         put the charIndex of the selectedText into theCharIndex
         repeat for each char theChar in the selectedText
            add 1 to theChars
         end repeat
         repeat for theChars
            delete char theCharIndex in tChar
         end repeat
         put numToChar(theKey) before char theCharIndex of tChar
         repeat for each char theChar in tChar
            put "*" after theAsterisk
         end repeat
         put theAsterisk into me
      else
         pass rawKeyDown
      end if
   else
      pass rawKeyDown
   end if
end rawKeyDown
still needs some adjuctments for the delete key but i'm sure you can manage it, when i fix it i'l be posting back
Knowledge is meant to be shared.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10354
Joined: Wed May 06, 2009 2:28 pm

Re: Password field.

Post by dunbarx » Thu Dec 18, 2014 6:33 am

Hi.

There have been several cute and clever ways to emulate this. But the simplest and best is still to set the imageSource to, say, "•".

Do a search for "imageSource". You will find several threads that deal with password fields.

Craig Newman

Post Reply