6 posts
                
             
            
                Posted 16 January 2013 - 10:05 AM
                I was writing an password program but anyone can write longer than 9 characters because I writed a nice gui but the characters will replace the characters from gui, so how I can limit characters writed by USER to for example 9?
                
             
         
        
        
            
            
                
                    
                
                2088 posts
                
                    
                        Location
                        South Africa
                    
                
             
            
                Posted 16 January 2013 - 10:06 AM
                Here's a limit read functinn
function limitRead(nLimit, replaceChar)
	term.setCursorBlink(true)
	local cX, cY = term.getCursorPos()
	local rString = ""
	if replaceChar == "" then replaceChar = nil end
	repeat
		local event, p1 = os.pullEvent()
		if event == "char" then
			-- Character event
			if #rString + 1 <= nLimit then
				rString = rString .. p1
				write(replaceChar or p1)
			end
		elseif event == "key" and p1 == keys.backspace and #rString >= 1 then
			-- Backspace
			rString = string.sub(rString, 1, #rString-1)
			xPos, yPos = term.getCursorPos()
			term.setCursorPos(xPos-1, yPos)
			write(" ")
			term.setCursorPos(xPos-1, yPos)
		end
	until event == "key" and p1 == keys.enter
	term.setCursorBlink(false)
	print() -- Skip to the next line after clicking enter.
	return rString
end
-- And you call it like this
input = limitRead(9)
-- If you want to replace it with a char, like read("*") then
password = limitRead(9, "*")
 
                
             
         
        
        
            
            
                
                    
                
                3790 posts
                
                    
                        Location
                        Lincoln, Nebraska
                    
                
             
            
                Posted 16 January 2013 - 10:08 AM
                Ha! I made a very similar limitRead() function.
                
             
         
        
        
            
            
                
                    
                
                2088 posts
                
                    
                        Location
                        South Africa
                    
                
             
            
                Posted 16 January 2013 - 10:10 AM
                Ha! I made a very similar limitRead() function.
Was it also called limitRead? xD haha
A lot of people ask this question… So I keep it there in my notepad++ in case I need it or others need a function like this…
 
                
             
         
        
        
            
            
                
                    
                
                3790 posts
                
                    
                        Location
                        Lincoln, Nebraska
                    
                
             
            
                Posted 16 January 2013 - 10:15 AM
                Actually yes, it was called limitRead! And mine is just bigger because it has some extra features, like background and text colors.
                
             
         
        
        
            
            
                
                    
                
                2088 posts
                
                    
                        Location
                        South Africa
                    
                
             
            
                Posted 16 January 2013 - 10:24 AM
                Actually yes, it was called limitRead! And mine is just bigger because it has some extra features, like background and text colors.
Haha nice :P/>