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

[LUA] [SOLVED] Need to make MAX length and MIN length.

Started by BabyCodder, 04 April 2013 - 01:47 AM
BabyCodder #1
Posted 04 April 2013 - 03:47 AM
Hi. I'm making a OS but for the new login and the login screen I want to do a max input and a minimum input. I either a link to a wiki post that would help me do this or a example code. Help is appreciated.
Zudo #2
Posted 04 April 2013 - 04:01 AM
Do you mean you want a 'read()' input with a minimum/maximum amount of characters?
If so look in the APIs section of this forum, I seem to remember there being something that did this.
JokerRH #3
Posted 04 April 2013 - 04:08 AM

local pass = read("*")
if #pass < 2 then
  print("Password too short")
elseif #pass > 10 then
  print("You want to be really secure, but this is too long.")
end
BabyCodder #4
Posted 04 April 2013 - 04:10 AM

local pass = read("*")
if #pass < 2 then
  print("Password too short")
elseif #pass > 10 then
  print("You want to be really secure, but this is too long.")
end
Thank you!
Telokis #5
Posted 04 April 2013 - 04:12 AM
You can check the input once the user typed it.
Example :


local input = nil
local minLength = 2
local maxLength = 10
while (input == nil or #input < minLength or #input > maxLength) do
	 input = read()
end

I think it should work as you expect.
JokerRH #6
Posted 04 April 2013 - 04:14 AM
You can check the input once the user typed it.
Example :


local input = nil
local minLength = 2
local maxLength = 10
while (input == nil or #input < minLength or #input > maxLength) do
	 input = read()
end

I think it should work as you expect.

No that won't because then you would have to type a char and hit enter before you can go on and you have no way to do a backslash.
It is possible but not that easy.
Telokis #7
Posted 04 April 2013 - 04:20 AM
You can check the input once the user typed it.
Example :


local input = nil
local minLength = 2
local maxLength = 10
while (input == nil or #input < minLength or #input > maxLength) do
	 input = read()
end

I think it should work as you expect.

No that won't because then you would have to type a char and hit enter before you can go on and you have no way to do a backslash.
It is possible but not that easy.

I don't understand why it shouldn't work. I modified it and tried, it seems to work as expected :


local input = nil
local minLength = 2
local maxLength = 10
while (input == nil) do
  write("Pass : ")
  input = read()
  if (#input < minLength or #input > maxLength) then
	print("Enter between "..minLength.." and "..maxLength.." characters please.")
	input = nil
  end
end
PixelToast #8
Posted 04 April 2013 - 04:40 AM
it looks perfect
though you dont need "= nil" on the first line
local input is enough

and you can simplify that while statement:

while not input do
anything not nil or false will pass as true on statements like these
Telokis #9
Posted 04 April 2013 - 04:45 AM
Ok, thanks Pixel. I wondered how it should be wrong.
I use to write

while (not input) do
When input is supposed to be false. When I want to check if it is nil, I prefer write it clearly. It's just because I am a C programer, I guess ! ^^
BabyCodder #10
Posted 04 April 2013 - 04:45 AM
This is what I have:
Spoiler

term.setBackgroundColor(colors.orange)
term.clear()
paintutils.drawLine(1,1,52,1, colors.lightBlue)
logo = paintutils.loadImage("logo")
paintutils.drawImage(logo, 23, 4)
term.setCursorPos(1,1)
term.setBackgroundColor(colors.lightBlue)
print(" [QUIT]")
term.setCursorPos(10,14)
term.setTextColor(colors.black)
term.setBackgroundColor(2)
term.write("New User Name:")
term.setCursorPos(25,14)
term.setBackgroundColor(1)
term.write("		  ")
term.setBackgroundColor(2)
term.setCursorPos(10,16)
term.write(" New Password:")
term.setBackgroundColor(1)
term.setCursorPos(25,16)
term.write("		  ")
term.setBackgroundColor(colors.lightBlue)
term.setCursorPos(27,18)
term.write("CREATE")
function new()
  event, button, x, y = os.pullEvent("mouse_click")
  Xmin = 2
  Xmax = 6
  Ymin = 1
  Ymax = 1
  UXmin = 25
  UXmax = 35
  UYmin = 14
  UYmax = 14
  PXmin = 25
  PXmax = 35
  PYmin = 16
  PYmax = 16
  LXmin = 27
  LXmax = 33
  LYmin = 18
  LYmax = 18
 
  term.setBackgroundColor(1)
 
  if button == 1 and x >= Xmin and x <= Xmax and y >= Ymin and y <= Ymax then
    os.shutdown()
  elseif button == 1 and x >= UXmin and x <= UXmax and y >= UYmin and y <= UYmax then
    term.setCursorPos(25,14)
    user = nil
    user = read()
	  if user == nil then
	    term.setCursorPos(16,15)
	    term.write("User Name Cannot Be Empty!")
	    new()
	  else
	    new()
	  end
  elseif button == 1 and x >= PXmin and x <= PXmax and y >= PYmin and y <= PYmax then
    term.setCursorPos(25,16)
    local pass = nil
    pass = read("*")
    if pass == nil then
	  term.setCursorPos(20,17)
	  term.write("Pass Cannot Be Empty!")
	  new()
    else
	  new()
    end
  elseif button == 1 and x >= LXmin and x <= LXmax and y >= LYmin and y <= LYmax then
	  fs.delete("user_stats")
	  fs.makeDir("user_stats")
	  pstats = fs.open("user_stats/pstats", "w")
	  ustats = fs.open("user_stats/ustats", "w")
	  pstats.write(pass)
	  ustats.write(user)
	  pstats.close()
	  ustats.close()
	  shell.run("login")
  else
    new()
  end
end
new()

It still lets the users input nothing. How can I stop this?
Telokis #11
Posted 04 April 2013 - 04:49 AM
If user inputs nothing, #input will value 0. You can check it.


    user = nil
    user = read()
		  if #user == 0 then
		    term.setCursorPos(16,15)
		    term.write("User Name Cannot Be Empty!")
		    new()
		  else
		    new()
		  end
PixelToast #12
Posted 04 April 2013 - 04:50 AM
because your comparing it to nil
an empty string isnt nil :P/>/>/&amp;gt;
replace

	if pass == nil then
with:

	if pass == "" then

Ninetainedo's method works too

EDIT:
you seem to be using recursion also .-.
please dont call functions within itself, that causes problems
use loops instead or else the program will stack overdflow after awhile
Telokis #13
Posted 04 April 2013 - 05:02 AM
EDIT:
you seem to be using recursion also .-.
please dont call functions within itself, that causes problems
use loops instead or else the program will stack overdflow after awhile

As just said Pixel, you shouldn't use recursion because of stack overflow.
I just want to quote the Codex of Error slaying (http://www.computerc...laying-2/#AIOOB) which says that:

Lua uses tail calls; what this means is that, if you call a function right after the return statement, the stack level will be re-used.


function asd()
  return asd()
end

If you do so, you won't have any stack overflow.
But, as said Pixel, you should use loops instead of recursion. I just say it because it doesn't represent a lot of changes in your code and it solves the stack issues.
You just have to put return before your recursive call.


return new()
BabyCodder #14
Posted 04 April 2013 - 05:05 AM
Well. If they click the inputs the initialize the input process it works. But if they don't even touch them but they do press create it lets them have nothing.
Telokis #15
Posted 04 April 2013 - 05:11 AM
You should edit this line :

elseif button == 1 and x >= LXmin and x <= LXmax and y >= LYmin and y <= LYmax then

You actually don't check your pass and user variables. If you want to show an error message, you have to check them.

Edit:

Moreover, I think you should put your variables before your function or, at least, before your first if.
BabyCodder #16
Posted 04 April 2013 - 05:14 AM
You should edit this line :

elseif button == 1 and x >= LXmin and x <= LXmax and y >= LYmin and y <= LYmax then

You actually don't check your pass and user variables. If you want to show an error message, you have to check them.
Oh yes! Thanks!