Jump to content

string format


Desaster

Recommended Posts

Posted

useful but i need an other thing I am making a edit box where you should put your the ammo you want and I want that the player just type numbers no text

Posted

what's the problem here ?

      local text = guiGetText ( ammotext ) 
      guiGridListSetItemText (ammoGirdcart, row2, 1, wepName1, false, true) 
      guiGridListSetItemText (ammoGirdcart, row2, 2, text, false, true) 
      guiGridListSetItemText (ammoGirdcart, row2, 3, wepCost1, false, true) 
      ) 

ps: this is in a fuction

Posted

This is an small example:

local curText = "" 
local editBox = guiCreateEdit ( 100, 50, 150, 150, "", false ) 
  
addEventHandler ( "onClientGUIChanged", editBox, 
    function ( ) 
        local text = guiGetText ( source ) 
        if ( text == "" ) then 
            curText = "0" 
            guiSetText ( source, "0" ) 
            return 
        end 
  
        if ( not tonumber ( text ) ) then 
            guiSetText ( source, curText ) 
        else 
            curText = text 
        end 
    end, 
    false 
) 

  • Moderators
Posted

Here is the more simple checker that you can do:

local editBox = guiCreateEdit(0.3,0.1,0.4,0.1,"",true) 
addEventHandler("onClientGUIChanged", editBox, function() 
    local text = guiGetText(source) or "" 
    if not tonumber(text) then --if the text isn't a number (statement needed to prevent infinite loop) 
        guiSetText(source, string.gsub(text, "%a", "")) --Remove all letters 
    end 
end) 

It will remove all letters that are written into a memo or an editbox. Here I used an editbox at line 1, be sure to paste this addEventHandler right after the creation of your editbox and replace the variable at line 2 to match your editbox variable.

EDIT: Oh and you only can write rounded numbers, you can't write a number like this: 100.5

Posted

if you want to remove stuff try string.gsub with custom patterns. i used this to change all caps to non-caps and hex colours.

(in this case, from an account data)

local named = getAccountData(playerAccount, "name") -- Get the string

local namedd = string.gsub(named, "#%x%x%x%x%x%x", "") --Remove Hex Colour

local named1 = string.gsub(namedd, "[A]", "a") -- start replacing characters

local named2 = string.gsub(named1, "", "b")

local named3 = string.gsub(named2, "[C]", "c")

local named4 = string.gsub(named3, "[D]", "d")

local named5 = string.gsub(named4, "[E]", "e")

local named6 = string.gsub(named5, "[F]", "f")

local named7 = string.gsub(named6, "[G]", "g")

local named8 = string.gsub(named7, "[H]", "h")

local named9 = string.gsub(named8, "", "i")

local named10 = string.gsub(named9, "[J]", "j")

local named11 = string.gsub(named10, "[K]", "k")

local named12 = string.gsub(named11, "[L]", "l")

local named13 = string.gsub(named12, "[M]", "m")

local named14 = string.gsub(named13, "[Ñ]", "ñ")

local named15 = string.gsub(named14, "[N]", "n")

local named16 = string.gsub(named15, "[O]", "o")

local named17 = string.gsub(named16, "[P]", "p")

local named18 = string.gsub(named17, "[Q]", "q")

local named19 = string.gsub(named18, "[R]", "r")

local named20 = string.gsub(named19, "", "s")

local named21 = string.gsub(named20, "[T]", "t")

local named22 = string.gsub(named21, "", "u")

local named23 = string.gsub(named22, "[V]", "v")

local named24 = string.gsub(named23, "[W]", "w")

local named25 = string.gsub(named24, "[X]", "x")

local named26 = string.gsub(named25, "[Y]", "y")

local named27 = string.gsub(named26, "[Z]", "z")

this is how it should work:

  
local Whatever-You-Want = string.gsub(string-To-Check, "Thingy-To-Search", "Remove-With")   
  

This is a list with all patterns: http://www.gammon.com.au/scripts/doc.ph ... tring.find

  • Moderators
Posted
But that won't remove the characters ( $, %, /, etc ).

Hmmm you right, then this:

local editBox = guiCreateEdit(0.3,0.1,0.4,0.1,"",true) 
addEventHandler("onClientGUIChanged", editBox, function() 
    local text = guiGetText(source) or "" 
    if tonumber(text) then return end 
    local newText = "" 
    for k=0, #text do 
        local char = string.sub(text, i, i) 
        if tonumber(char) then --it's a number 
            newText..char 
        end 
    end 
    guiSetText(source, newText) 
end) 

Posted

It's never going to work like you think it will. I've tried the same many times and by spamming my keyboard it always adds another character which I don't want. I think you should just allow all characters and then strip them off later.

Posted

I dont know if this is going to work, but you can try the combination of onClientGUIFocus, onClientGUIBlur and onClientKey with cancelEvent()

Posted
how to enable a sting format like I want just numbers in my edit box if you type any other thing it won't be written
yourGUIEdit = guiCreateEdit( ... ) 
  
addEventHandler( "onClientGUIChanged", yourGUIEdit, function() 
  local currText = guiGetText( source ) 
  local newText = string.gsub( currText, '[^0-9]', '' ) 
  if newText ~= currText then 
    guiSetText( source, newText ) 
  end 
end, false ) 

  • 2 weeks later...
Posted (edited)

Hello, I am bumping this topic as I found a really nice and efficient way of allowing only numbers in edit fields ( or anything else you want ).

guiSetProperty(editBox, "ValidationString", "^[0-9]*$") 

This is just an example, it uses regex and can be used for any kind of input filtering.

Edited by Guest
Posted
Hello, I am bumping this topic as I found a really nice and efficient way of allowing only numbers in edit fields ( or anything else you want ).
guiSetProperty(editBox, "ValidationString", "^[0-9]*$") 

This is just an example, it uses regex and can be used for any kind of input filtering.

Thank you.

Posted
guiSetProperty(editBox, "ValidationString", "^[0-9]*$") 

hmmm I tested this and I found out that it has a bug you can copy any text and paste it in it and then you have a text written in it :P (bad) the best one is that one with tonumber

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...