Jump to content

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


Recommended Posts

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

Edited by Metin2 Dev
Core X - External 2 Internal
Link to comment
Share on other sites

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
Share on other sites

  • Developer

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
Share on other sites

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
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now

Announcements



×
×
  • 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.