Hugos Posted August 28, 2019 Share Posted August 28, 2019 I have a "guiCreateEdit," I need to prohibit the user from entering any characters except numbers. And I need to bring them out using "dxDrawText," but replacing them with "*" (asterisk). How do I do that? addEvent("edit", true) function editbox() PIN = guiCreateEdit(0, 0, 0, 0, "", false) end addEventHandler("edit", resourceRoot, edit) function pin() dxDrawText(guiGetText(PIN), 0, 0, 100, 100, tocolor(0, 0, 0), 1, Font, "center", "center", true, true, true) end (It is string.gsub necessary to use? ) Link to comment
Moderators IIYAMA Posted August 28, 2019 Moderators Share Posted August 28, 2019 (edited) 38 minutes ago, Hugos said: (It is string.gsub necessary to use? ) Is quickest method, but not the only way. https://stackoverflow.com/questions/47766700/how-to-extract-numbers-from-a-string-in-Lua Edited August 28, 2019 by IIYAMA 1 Link to comment
Scripting Moderators ds1-e Posted August 28, 2019 Scripting Moderators Share Posted August 28, 2019 1 hour ago, Hugos said: I have a "guiCreateEdit," I need to prohibit the user from entering any characters except numbers. And I need to bring them out using "dxDrawText," but replacing them with "*" (asterisk). How do I do that? addEvent("edit", true) function editbox() PIN = guiCreateEdit(0, 0, 0, 0, "", false) end addEventHandler("edit", resourceRoot, edit) function pin() dxDrawText(guiGetText(PIN), 0, 0, 100, 100, tocolor(0, 0, 0), 1, Font, "center", "center", true, true, true) end (It is string.gsub necessary to use? ) 1. local character local forbidden_characters = { ["w"] = "w", ["a"] = "a", ["s"] = "s", ["d"] = "d", } function onClientCharacter(char) if forbidden_characters[char] then character = char end end addEventHandler("onClientCharacter", getRootElement(), onClientCharacter) function onClientGUIChanged(element) if element then -- add check for your element local gui_text = guiGetText(element) if gui_text then local forbidden_character = character local check = forbidden_characters[forbidden_character] if check then local replace = string.gsub(gui_text, check, "") guiSetText(element, replace) end end end end addEventHandler("onClientGUIChanged", getRootElement(), onClientGUIChanged) Tested a bit, and looks like works for typing, you would need to replace string with characters to "" when player paste something. Also you need to fill table with forbidden characters. https://wiki.multitheftauto.com/wiki/OnClientPaste 1 Link to comment
Hugos Posted August 29, 2019 Author Share Posted August 29, 2019 (edited) All right, I already figured it out. Everything is much easier... pin = ("" ..string.gsub(guiGetText(PIN), "%D", "").. "") guiSetText(PIN, pin) string.gsub - we replace in line "%D" on "" , and all. guiGetText(PIN) - My line with we enter the text; "%D" - Any letter or symbol other than digit; "" - emptiness; Edited August 29, 2019 by Hugos 1 Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now