i.e: Username: 12345678910
The allowed characters would be limited to ten and no more. Can anyone help me?
local function read()
local input = ''
local x,y = term.getCursorPos()
term.setCursorBlink(true)
repeat
term.setCursorPos(x,y)
term.write(input)
local ev, p1 = os.pullEvent()
if ev == 'char' then
if #input < 10 then
input = input .. p1
end
elseif ev == 'key' then
if p1 == keys.backspace then
input = input:sub(1, #input - 1)
end
end
until ev == 'key' and p1 == keys.enter
term.setCursorBlink(false)
return input
end
local input
while true do
input = read()
if #input > 10 then
print "10 character max length"
else
break
end
end
print("You typed: "..input)
That is not the way I want the limit to be. It needs to limit how much they can type. Your's was a check after they hit enter. It needs to prevent them from typing once they type ten characters. If you have ever used a database you would know what I need. In the database you can limit how many characters are allowed to be typed within the text box.Why did you double post the exact same question? I already answered yours here: http://www.computerc...x-input-length/
local function read()
local input = ''
local x,y = term.getCursorPos()
term.setCursorBlink(true)
repeat
term.setCursorPos(x,y)
term.write(input)
local ev, p1 = os.pullEvent()
if ev == 'char' then
if #input < 10 then
input = input .. p1
end
elseif ev == 'key' then
if p1 == keys.backspace then
input = input:sub(1, #input - 1)
end
end
until ev == 'key' and p1 == keys.enter
term.setCursorBlink(false)
return input
end
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(10)
-- If you want to replace it with a char, like read("*") then
password = limitRead(10, "*")
Doesn't necessarily limit it the way I want it to, but I guess it will do for nowRead the other post,local function read() local input = '' local x,y = term.getCursorPos() term.setCursorBlink(true) repeat term.setCursorPos(x,y) term.write(input) local ev, p1 = os.pullEvent() if ev == 'char' then if #input < 10 then input = input .. p1 end elseif ev == 'key' then if p1 == keys.backspace then input = input:sub(1, #input - 1) end end until ev == 'key' and p1 == keys.enter term.setCursorBlink(false) return input end
I did not even see that. It looks very promising, I'll say if it works out. Thanks for the help.Read the other post,local function read() local input = '' local x,y = term.getCursorPos() term.setCursorBlink(true) repeat term.setCursorPos(x,y) term.write(input) local ev, p1 = os.pullEvent() if ev == 'char' then if #input < 10 then input = input .. p1 end elseif ev == 'key' then if p1 == keys.backspace then input = input:sub(1, #input - 1) end end until ev == 'key' and p1 == keys.enter term.setCursorBlink(false) return input end
Edit:
This is one that I made a while back which I use now and then,Spoiler
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(10) -- If you want to replace it with a char, like read("*") then password = limitRead(10, "*")
You don't read do you? Mine is exactly the same as remiX's <_</> . My example limits while typing and even happens to limit on exactly 10 characters by default. And how can you assume that I never used a database? :P/> And after all, you still double posted for the same question.That is not the way I want the limit to be. It needs to limit how much they can type. Your's was a check after they hit enter. It needs to prevent them from typing once they type ten characters. If you have ever used a database you would know what I need. In the database you can limit how many characters are allowed to be typed within the text box.Why did you double post the exact same question? I already answered yours here: http://www.computerc...x-input-length/
local function readN(len, replaceChar)
len = len or 10
local input=""
local key = 0
term.setCursorBlink(true)
repeat
local e,p1 = os.pullEvent()
if e=="char" then
if #input < len then
input = input .. p1
term.write(replaceChar or p1)
end
elseif e=="key" and p1==keys.backspace and #input > 0 then
input = input:sub(1,#input-1)
local x,y = term.getCursorPos()
term.setCursorPos(x-1,y)
term.write(" ")
term.setCursorPos(x-1,y)
end
until p1==keys.enter
term.setCursorBlink(false)
return input
end
I'm going to stop listening to you Orwell since you got a bit lippy. If I posted twice that usually means the person that replied the first time did not answer my question accordingly. I also don't recall saying you never used a database, I was simply saying "if you have used one, then you know what I want". Thank you again Remix :)/>You don't read do you? Mine is exactly the same as remiX's <_</> . My example limits while typing and even happens to limit on exactly 10 characters by default. And how can you assume that I never used a database? :P/> And after all, you still double posted for the same question.That is not the way I want the limit to be. It needs to limit how much they can type. Your's was a check after they hit enter. It needs to prevent them from typing once they type ten characters. If you have ever used a database you would know what I need. In the database you can limit how many characters are allowed to be typed within the text box.Why did you double post the exact same question? I already answered yours here: http://www.computerc...x-input-length/
Here is the code I linked you to. It's short and easy to understand (still quite the same as remiX's though):local function readN(len, replaceChar) len = len or 10 local input="" local key = 0 term.setCursorBlink(true) repeat local e,p1 = os.pullEvent() if e=="char" then if #input < len then input = input .. p1 term.write(replaceChar or p1) end elseif e=="key" and p1==keys.backspace and #input > 0 then input = input:sub(1,#input-1) local x,y = term.getCursorPos() term.setCursorPos(x-1,y) term.write(" ") term.setCursorPos(x-1,y) end until p1==keys.enter term.setCursorBlink(false) return input end
Edit: also, remiX's version was also posted by him in the thread I linked to, so it's not us misunderstanding your question.
Edit: also, remiX's version was also posted by him in the thread I linked to, so it's not us misunderstanding your question.