Derma DTextEntry

Hey, I am working on a DTextEntry panel and I am trying to limit the characters inserted.


local enter_law = vgui.Create("DTextEntry", frame)
enter_law:SetPos(25, 460)
enter_law:SetSize(450, 30)
enter_law:SetText("Enter law text.") -- Initial message displayed
enter_law.OnTextChanged = function(self)
	local text = self:GetText()
	local amount = string.len(text)
	if amount > 123 then
		self:SetText(self.OldText)
		self:SetValue(self.OldText)
	else
		self.OldText = text
	end
end
enter_law.OnEnter = function(self)
	self:Remove()
	local counter = 0 
	for k,v in pairs(laws) do
		counter = counter + 1
	end
	if banned_text[self:GetText()] then
		net.Start("invalid_input")
			net.WriteString("Invalid input.")
		net.SendToServer()
	elseif counter < 12 then
		table.insert(laws, (counter + 1)..'. '..self:GetText())
		net.Start("laws_update")
			net.WriteString("A law has been added!")
		net.SendToServer()
	else
		net.Start("max_laws")
			net.WriteString("You have hit the law limit.")
		net.SendToServer()
	end
end

So far, It limits the characters the way I want, the only problem is if the user pastes a line in that is longer than 123 characters with the initial message I have shown then it errors out. How can I make it so that when the user first clicks to edit the text the initial text is deleted. I can’t find anything for it in the DTextEntry wiki… I tried playing around with OnMousePressed but I didn’t really get anywhere.

I have only pasted a part of my code so if there are undefined variables they are defined elsewhere.

What do you mean by error out?
If you want the text to be less than 123 characters, just check the length of the text OnEnter, if it’s less than 123 start your net message, if not do something like LocalPlayer():ChatPrint(“This shit is too long”) or something similar. Hopefully that helps?

[editline]21st February 2017[/editline]

Also you don’t need to use a counter variable you can return the # of values by doing #laws or #TableName

As well, its not self:GetText its self:GetValue() and just dont even have text on there already, add a little label next too it or something.

Adding up.
Why are you writing the strings to the server like that?
Its a lot of networking, and in addition the checks in your server would be terrible.
U better off send numbers, and analyse it on the server.

The wiki says it can be either or, GetValue() has a larger byte size allowance but I am limiting the textual input so I don’t need to worry about the size being too large.

[editline]21st February 2017[/editline]

The character limiting function I have is made so that you don’t have to restart your input if you go over the limit, They are able to edit their line and adjust if they want. The reason it errors out is because if they paste a line and the string.len() is over 123 characters with the initial message it calls the self.oldtext before it has a chance to be defined.

[editline]21st February 2017[/editline]

This is the first time ive worked with net messages so I am very new to it all, I don’t quite understand what you are saying here could you give me an example?? Thanks!

If you don’t want to have them reenter their message just don’t call self:Remove() unless their text is less than 123. Sorry, I might be reading this incorrectly.

Add me on steam, (link in profile), i’ll help you out :slight_smile: