This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
SuicidalSTDz's profile picture

Max input length

Started by SuicidalSTDz, 01 January 2013 - 04:59 PM
SuicidalSTDz #1
Posted 01 January 2013 - 05:59 PM
I need to set the max input length within a box to 10.
i.e: Username: 12345678910
The allowed characters would be limited to ten and no more. Can anyone help me?
Kingdaro #2
Posted 01 January 2013 - 06:10 PM
You could just make a custom read() function. There's probably a way to do it with the base read(), but I'm not sure of a way.


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

Or, with using the base read(), don't take any input until the user has entered a string under 10 characters (while telling the user that it needs to be under ten characters).


local input
while true do
  input = read()
  if #input > 10 then
    print "10 character max length"
  else
    break
  end
end
print("You typed: "..input)
Orwell #3
Posted 02 January 2013 - 01:08 AM
Kingdaro's solution is good. I just wanted to point out that this question was posed 2 days ago:
http://www.computercraft.info/forums2/index.php?/topic/7782-how-to-restrict-the-length-of-a-word/
Try using the search function.
SuicidalSTDz #4
Posted 02 January 2013 - 10:30 AM
I am developing a Lock program and need to limit the amount of Characters the user is permitted to type while putting in their username. It needs to restrict it before they hit enter, not after.

For example if I were to type SuicidalSTDz, it would stop letting me type after the tenth character.

If you know how to do this, I am all ears.
Orwell #5
Posted 02 January 2013 - 10:47 AM
Why did you double post the exact same question? I already answered yours here: http://www.computercraft.info/forums2/index.php?/topic/7945-max-input-length/
SuicidalSTDz #6
Posted 02 January 2013 - 10:57 AM
Why did you double post the exact same question? I already answered yours here: http://www.computerc...x-input-length/
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.
remiX #7
Posted 02 January 2013 - 11:09 AM
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, "*")
SuicidalSTDz #8
Posted 02 January 2013 - 11:24 AM
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
Doesn't necessarily limit it the way I want it to, but I guess it will do for now
remiX #9
Posted 02 January 2013 - 11:25 AM
And the one I posted in my edit?
SuicidalSTDz #10
Posted 02 January 2013 - 11:43 AM
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, "*")
I did not even see that. It looks very promising, I'll say if it works out. Thanks for the help.
Orwell #11
Posted 02 January 2013 - 11:58 AM
Why did you double post the exact same question? I already answered yours here: http://www.computerc...x-input-length/
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.
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.

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.
SuicidalSTDz #12
Posted 02 January 2013 - 03:08 PM
Why did you double post the exact same question? I already answered yours here: http://www.computerc...x-input-length/
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.
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.

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.
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 :)/>
Orwell #13
Posted 02 January 2013 - 03:19 PM
First off, you're not following the rules by opening a new thread in stead of explaining yourself in the first one. Second, I'm telling you that your question was answered there. I posted this link there, and what do you see at post #8 of that thread? The script of remiX you're using now. So you have been helped in the first post. It's just basic courtesy to acknowledge the people trying to help you. You can stop listening to me now, I've said what I had to say.
ChunLing #14
Posted 02 January 2013 - 03:44 PM
Okay, Orwell isn't the one that can be accused of being "lippy" here. That term would be more accurately applied to someone who spouts off without having an adequate understanding of the subject at hand, which would be who? Not Orwell, or at least not on this topic.
Lyqyd #15
Posted 02 January 2013 - 05:40 PM
Threads merged. Please do not double-post the same question, especially not only a day after the first post.
remiX #16
Posted 02 January 2013 - 07:55 PM
Edit: also, remiX's version was also posted by him in the thread I linked to, so it's not us misunderstanding your question.

Yeah, I just fixed it and halved the code just about lol