Jump to content




Limiting amount of characters to type?


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

#1 brett122798

  • Members
  • 300 posts
  • LocationIn the TARDIS at an unknown place in time.

Posted 29 October 2012 - 04:15 AM

Hi, I want to know how I could make an input limited on characters.



Side Question: How can I measure the amount of characters in a variable?

#2 ChunLing

  • Members
  • 2,027 posts

Posted 29 October 2012 - 05:29 AM

#varname returns the number of characters in a string or elements in the top layer of a table.

So, you can do a couple of things. You can have a repeat varname = read() until #varname < limit, you can just get the input and then if #varname < limit then varname = varname:sub(1,limit) end, or a number of other things. The loop is good if you want to make sure that the user inputs the shorter string, the other okay if you just want the string shortened for some reason.

#3 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

Posted 29 October 2012 - 06:33 PM

Yeah so something like this

term.clear() term.setCursorPos(1,1)

repeat
    var = read()
until #var < 11

print(var)


#4 ChunLing

  • Members
  • 2,027 posts

Posted 29 October 2012 - 08:33 PM

Well...you do want to have a prompt that says something about the 10 character limit. I don't know if you would want to put that in the loop or before it, just as long as it's somewhere so the user knows what they're supposed to do.





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users