Jump to content
  • 0

Check if a string contains only numbers and letters (without symbols)


DaNy3LL

Question

Hello guys, I tried to make a quest and I encountered a problem

I need to check if a string entered by the player contains only letters and/or numbers (without symbols)

 

I tried something like this , but it doesn't work ...

		when button or info begin
			say_title("Account Security:")
			say("This is used as a protection measure to prevent item")
			say("loss in case of your account being hacked/stolen.")
			say("")
			say_reward("If the protection is active you can't drop, sell, or exchange or use any item.")
			wait()
			say_title("Account Security:")
			say("Now please choose a password, so you can")
			say("manage this protection later.")
			say_orange("The password can only contain letters and numbers")
			say_orange("Your must use at least 6 characters, but no more")
			say_orange("than 12.")
			wait()
			say_title("Account Security:")
			say("Please enter a password :")
			local password = tostring(input())
			if (validate_as_password(password) == true) then
				say("Your password is now "..password.."")
				set_as_password(password)
				send_letter("~Account Security")
			else
				say("Your password is either too long or too short or")
				say("contains symbols. Try again.")
				send_letter("~Account Security")
				return
			end
                end

Those are the functions validate_as_password and set_as_password

function set_as_password(password)
	local a = mysql_query("INSERT INTO account.account_security values ('"..pc.get_player_id().."', '"..password.."')")
end

function validate_as_password(password)
	if string.len(password) < 6 or string.len(password) > 12 then
		return false
	else
		if(password:match("%W")) then
			return false
		else
			return true
		end
	end
end

Here is a gif with the quest

 

61adbc55f5364aff5b9c663c81570143.gif

 

The function set_as_password works, but validate_as_password doesn't. The query runs, even if I enter symbols.

 

When I enter a password which should be correct :

 

5a450a286efcd27496220ddd0aac3676.png

 

When I enter a password which should be incorrect :

 

12fcfba2f58c3bd786a003fe82d78a9b.png

 

Kind Regards, DaNy3LL

Link to comment

10 answers to this question

Recommended Posts

  • 0

Try with this:

function validate_as_password(pw)
	return (string.len(pw) >= 6 and string.len(pw) <= 12) and (string.match(pw, "%W") == nil)
end
  • Love 1

when you return 0 and server doesn't boot:

unknown.png

Link to comment
  • 0

This should work:

        when button or info begin
            say_title("Account Security:")
            say("This is used as a protection measure to prevent item")
            say("loss in case of your account being hacked/stolen.")
            say("")
            say_reward("If the protection is active you can't drop, sell, or exchange or use any item.")
            wait()
            say_title("Account Security:")
            say("Now please choose a password, so you can")
            say("manage this protection later.")
            say_orange("The password can only contain letters and numbers")
            say_orange("Your must use at least 6 characters, but no more")
            say_orange("than 12.")
            wait()
            say_title("Account Security:")
            say("Please enter a password :")
            local password = tostring(input())
			if (string.len(password) >= 6 and string.len(password) <= 12) and (string.match(password, "%W") == nil) then			
                say("Your password is now "..password.."")
				mysql_query("INSERT INTO account.account_security values ('"..pc.get_player_id().."', '"..password.."')")
                send_letter("~Account Security")
            else
                say("Your password is either too long or too short or")
                say("contains symbols. Try again.")
                send_letter("~Account Security")
                return
            end
        end
Link to comment
  • 0

Actually it wouldn't Denis, 'cause here:

if (string.len(pw) >= 6 and string.len(pw) <= 12) and (string.match(pw, "%W") == nil) then

pw wasn't declared, and you want to check the password variable content, so instead of pw it must be password. Also, maybe I'm wrong, but some special characters (e.g: say("~smth~")) don't work, so I guess you might add a space at the beginning of your send_letter(). Anyway, you should check what your syserr says.

  • Love 1

when you return 0 and server doesn't boot:

unknown.png

Link to comment
  • 0

Actually it wouldn't Denis, 'cause here:

if (string.len(pw) >= 6 and string.len(pw) <= 12) and (string.match(pw, "%W") == nil) then

pw wasn't declared, and you want to check the password variable content, so instead of pw it must be password. Also, maybe I'm wrong, but some special characters (e.g: say("~smth~")) don't work, so I guess you might add a space at the beginning of your send_letter(). Anyway, you should check what your syserr says.

 

Yes I forgot to change the variable, thanks.

Link to comment
  • 0

In syserr I had

RunState: LUA_ERROR: locale/uk/quest/questlib.lua:2053: attempt to call field `match' (a nil value)

And I modified string.match to string.find and now works

function validate_as_password(pw)
    return (string.len(pw) >= 6 and string.len(pw) <= 12) and (string.find(pw, "%W") == nil)
end
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


×
×
  • Create New...

Important Information

Terms of Use / Privacy Policy / Guidelines / We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.