Jump to content




Max input length



  • You cannot reply to this topic
15 replies to this topic

#1 SuicidalSTDz

    Permutator of Strings

  • Members
  • 1,308 posts
  • LocationPennsylvania

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?

#2 Kingdaro

    The Doctor

  • Members
  • 1,636 posts
  • Location'MURICA

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)


#3 Orwell

    Self-Destructive

  • Members
  • 1,091 posts

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.computerc...ngth-of-a-word/
Try using the search function.

#4 SuicidalSTDz

    Permutator of Strings

  • Members
  • 1,308 posts
  • LocationPennsylvania

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.

#5 Orwell

    Self-Destructive

  • Members
  • 1,091 posts

Posted 02 January 2013 - 10:47 AM

Why did you double post the exact same question? I already answered yours here: http://www.computerc...x-input-length/

#6 SuicidalSTDz

    Permutator of Strings

  • Members
  • 1,308 posts
  • LocationPennsylvania

Posted 02 January 2013 - 10:57 AM

View PostOrwell, on 02 January 2013 - 10:47 AM, said:

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.

#7 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

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


#8 SuicidalSTDz

    Permutator of Strings

  • Members
  • 1,308 posts
  • LocationPennsylvania

Posted 02 January 2013 - 11:24 AM

View PostremiX, on 02 January 2013 - 11:09 AM, said:

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

#9 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

Posted 02 January 2013 - 11:25 AM

And the one I posted in my edit?

#10 SuicidalSTDz

    Permutator of Strings

  • Members
  • 1,308 posts
  • LocationPennsylvania

Posted 02 January 2013 - 11:43 AM

View PostremiX, on 02 January 2013 - 11:09 AM, said:

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
I did not even see that. It looks very promising, I'll say if it works out. Thanks for the help.

#11 Orwell

    Self-Destructive

  • Members
  • 1,091 posts

Posted 02 January 2013 - 11:58 AM

View PostSuicidalSTDz, on 02 January 2013 - 10:57 AM, said:

View PostOrwell, on 02 January 2013 - 10:47 AM, said:

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.

#12 SuicidalSTDz

    Permutator of Strings

  • Members
  • 1,308 posts
  • LocationPennsylvania

Posted 02 January 2013 - 03:08 PM

View PostOrwell, on 02 January 2013 - 11:58 AM, said:

View PostSuicidalSTDz, on 02 January 2013 - 10:57 AM, said:

View PostOrwell, on 02 January 2013 - 10:47 AM, said:

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 :)

#13 Orwell

    Self-Destructive

  • Members
  • 1,091 posts

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.

#14 ChunLing

  • Members
  • 2,027 posts

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.

#15 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

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.

#16 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

Posted 02 January 2013 - 07:55 PM

View PostOrwell, on 02 January 2013 - 11:58 AM, said:

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





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users