Jump to content




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


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

#1 BabyCodder

  • Members
  • 87 posts

Posted 04 April 2013 - 02: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.

#2 Zudo

  • Members
  • 800 posts
  • LocationUK

Posted 04 April 2013 - 03: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.

#3 JokerRH

  • Members
  • 147 posts

Posted 04 April 2013 - 03: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


#4 BabyCodder

  • Members
  • 87 posts

Posted 04 April 2013 - 03:10 AM

View PostJokerRH, on 04 April 2013 - 03:08 AM, said:

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!

#5 Telokis

  • Members
  • 88 posts
  • LocationToulouse, France

Posted 04 April 2013 - 03: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.

#6 JokerRH

  • Members
  • 147 posts

Posted 04 April 2013 - 03:14 AM

View PostNinetainedo, on 04 April 2013 - 03:12 AM, said:

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.

#7 Telokis

  • Members
  • 88 posts
  • LocationToulouse, France

Posted 04 April 2013 - 03:20 AM

View PostJokerRH, on 04 April 2013 - 03:14 AM, said:

View PostNinetainedo, on 04 April 2013 - 03:12 AM, said:

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


#8 PixelToast

  • Signature Abuser
  • 2,265 posts
  • Location3232235883

Posted 04 April 2013 - 03: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

#9 Telokis

  • Members
  • 88 posts
  • LocationToulouse, France

Posted 04 April 2013 - 03: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 ! ^^

#10 BabyCodder

  • Members
  • 87 posts

Posted 04 April 2013 - 03:45 AM

This is what I have:
Spoiler

It still lets the users input nothing. How can I stop this?

#11 Telokis

  • Members
  • 88 posts
  • LocationToulouse, France

Posted 04 April 2013 - 03: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


#12 PixelToast

  • Signature Abuser
  • 2,265 posts
  • Location3232235883

Posted 04 April 2013 - 03:50 AM

because your comparing it to nil
an empty string isnt nil :P/>/&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

#13 Telokis

  • Members
  • 88 posts
  • LocationToulouse, France

Posted 04 April 2013 - 04:02 AM

View PostPixelToast, on 04 April 2013 - 03:50 AM, said:

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:

Quote

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


#14 BabyCodder

  • Members
  • 87 posts

Posted 04 April 2013 - 04: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.

#15 Telokis

  • Members
  • 88 posts
  • LocationToulouse, France

Posted 04 April 2013 - 04: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.

#16 BabyCodder

  • Members
  • 87 posts

Posted 04 April 2013 - 04:14 AM

View PostNinetainedo, on 04 April 2013 - 04:11 AM, said:

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!





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users