Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to check if a textbox is Empty?

How would I Check if an Textbox's Text was empty, I've tried this:

if tostring(Text) == nil then
	print("Failed to Respond")
end

4 answers

There are multiple ways to go about doing this:

string.len

The string library gives a great way to determine the length of the text. It is the len method in the string library - short for length.

local text = "Hello, World!";
print(string.len(text))

All strings have a metatable attached to the original string library which means you can do this aswell:

local text = "Hello, World!";
print(text:len())

Using the length operator

If you are familiar with the length operator, mainly seen as # in Lua, you realize that it gets you the length of arrays. Strings are technically arrays because they are arrays of characters.

Thus you can do:

local text = "Hello, World!";
print(#text)	

Simple Conditional

You can also just check if your string equals an empty string.

local text = "";
assert(text ~= "", "Empty!")

Removing Whitespace

More than likely there will be times where a user will just spam a bunch of spaces into your TextBox. We need to get rid of these spaces because Lua [and every other language] still counts spaces as a character length.

We can use string substitution.

local text = "         ";
text = text:gsub("[%s]","")  --Remove all whitespace
print("Empty? ", #text == 0)

Useful Links

String Pattern Complements: http://wiki.roblox.com/index.php?title=String_pattern#Complements

String Manipulation: http://wiki.roblox.com/index.php?title=Function_dump/String_manipulation#string.len

Strings: http://wiki.roblox.com/index.php?title=String

if textbox.Text=="" then
	print("NOTHING ENTERED")

elseif textbox.Text==" "
	print("NOTHING ENTERED")
else
	print(textbox.Text)
end

Just use an if statement.

if text == "" then
	print("Textbox is empty")
end

if text == "" then

1 comment

0

That WAS my idea, but I was pretty sure there was a twist.

This discussion is preserved as it was. New answers, comments, and votes are closed.