Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to check if a textbox is Empty?

Asked by 8 years ago

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

Log in to vote
4
Answered by 8 years ago

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

Ad
Log in to vote
0
Answered by 6 years ago
if textbox.Text=="" then
    print("NOTHING ENTERED")

elseif textbox.Text==" "
    print("NOTHING ENTERED")
else
    print(textbox.Text)
end
Log in to vote
-1
Answered by 8 years ago

Just use an if statement.

if text == "" then
    print("Textbox is empty")
end
Log in to vote
-2
Answered by 8 years ago

if text == "" then

0
That WAS my idea, but I was pretty sure there was a twist. RadiantSolarium 35 — 8y

Answer this question