Text Fields
Overview
A text field is a box where a person types one line of text, like a name, a search word or a password. It's one of the most common controls in any program. In this tutorial, you will add text fields to a page, read what a person typed, check it before you use it, decide what to do when it isn't right, and update the page while the person types.
Goals
You have the following goals for this tutorial:
By the end of this lesson, you can
- Add a text field to a page and give it a clear label, name and description
- Read the text with
GetTextand change it withSetText - Check what a person typed before using it, and compare ways to handle input that isn't right
- Respond to each change in a text field with a
TextChangeListener - Find who a checking rule leaves out, and change the rule to fix it
- Explain what a password field protects against, and what it costs
Warm up
Think of a sign-up page you have used. What did it do when you left a box empty, or typed something it didn't like? Was the message helpful? Where on the page did the message appear, and how would someone who can't see the screen find it?
Vocabulary
You will be learning about the following vocabulary words:
| Term | Definition |
|---|---|
| Text field | A control where a person types a single line of text. |
| Caret | The blinking line that shows where the next letter you type will go. It is sometimes called the text cursor. |
| Input validation | Checking what a person typed before the program uses it, for example making sure a box is not empty. |
| Data quality | How correct, complete and usable a piece of data is. A name typed with extra spaces, or a box left empty, is a data quality problem. |
| Listener | An object that a control calls when something happens to it. A TextChangeListener is called each time the text changes. |
| Password field | A text field that hides what is typed, showing stars instead of the letters. |
| Bias | When a rule or program treats some groups of people unfairly, often by accident, for example a name check that rejects real names. |
| Security measure | Something that protects people, data or devices. Some protect against people nearby, such as hiding a password on the screen; others protect data inside the computer. |
New Code to Learn
You will be using the following new pieces of code:
| Quorum Code | Code Example | Explanation |
|---|---|---|
| AddTextField(text name) | TextField field = page:AddTextField("Your first name") | Adds a text field to a page. The name is what a screen reader says for the field. |
| GetTextField(text name) | TextField field = page:GetTextField("Your first name") | Finds a text field on a page by its name. |
| GetText() | text typed = field:GetText() | Returns all of the text in the field. |
| SetText(text value) | field:SetText("Player 1") | Replaces the text in the field. |
| Empty() | field:Empty() | Removes all of the text from the field. |
| SetPassword(boolean value) | field:SetPassword(true) | Hides the typed text, for passwords. |
| AddTextChangeListener(TextChangeListener listener) | field:AddTextChangeListener(listener) | Calls the listener's TextChanged action every time the text changes. |
| Trim() | text cleaned = typed:Trim() | Returns the text with spaces removed from the start and end. |
CSTA Standards
This tutorial covers the following CSTA standards:
For teachers · 2026 CSTA K–12 Computer Science Standards
High School · Algorithms & Design
Evaluate the ethical implications, societal impacts, and potential biases of rule-based and data-driven algorithms.
Only this lesson so far
High School · Data & Analysis
Evaluate different approaches to verifying consistency and compliance with expected data types, values, and ranges.
Also in 3 other lessons
High School · Systems & Security
Identify different types of cybersecurity and physical security measures and the trade-offs for users, data, and devices.
Only this lesson so far
Middle School · Algorithms & Design
Modify an algorithm to address a specific societal impact, ethical issue, or bias.
Only this lesson so far
Middle School · Data & Analysis
Analyze options to address a data quality issue.
Also in 2 other lessons
Specialty I · Software Development
Design a test plan that exercises program functionality, including edge cases, error conditions, and varied user inputs.
Also in 2 other lessons
Explore
Text fields and text boxes
Quorum has two controls for typing:
- a text field (
TextField) holds one line of text. Use it for short answers like a name or a search; - a text box (
TextBox) holds many lines. Use it for longer writing, like notes or code. The Text Boxes tutorial covers it.
When a text field has the focus, a blinking line called the caret shows where the next letter will go. A person can type, select text and use the usual editing keys:
| Keys | What they do |
|---|---|
| Left Arrow, Right Arrow | Move the caret one letter |
| Home or Up Arrow | Move the caret to the start |
| End or Down Arrow | Move the caret to the end |
| Shift with any of the keys above | Select text as the caret moves |
| Backspace, Delete | Delete a letter, or the selected text |
| Ctrl+A | Select all of the text |
| Ctrl+C, Ctrl+X, Ctrl+V | Copy, cut and paste |
| Ctrl+Z, Ctrl+Y | Undo and redo |
| Tab, Shift+Tab | Leave the field and move the focus |
On a Mac, use Cmd in place of Ctrl.
Making a text field on a page
A page's AddTextField action makes a text field and adds it to the page. The
text you pass in is the field's name. A screen reader says the name when the
field gets the focus, so the name should say what to type. A description can
add a hint.
The field itself shows no words, so a person who can see the screen needs a label too. When you add a label right before a text field, the page puts the label beside the field, or above it if there isn't room.
This program asks for a name and has a button that says hello. A second label, named "Greeting", is where the program shows its answer.
use Libraries.Interface.Forms.Form
use Libraries.Interface.Forms.Page
use Libraries.Interface.Controls.TextField
use Libraries.Interface.Controls.Button
Form form
Page page = form:GetMainPage()
page:SetTitle("Greeter")
page:AddLabel("First name:")
TextField name = page:AddTextField("Your first name")
name:SetDescription("Type your first name, then press Say hello.")
Button hello = page:AddButton("Say hello")
page:AddLabel("Greeting", "No greeting yet.")
HelloBehavior behavior
hello:SetBehavior(behavior)
form:Display()
Reading and checking the text
GetText returns everything in the field. Before you use it, check it. This
is called input validation. Here, the program uses Trim to remove spaces
from the start and end, so that a field with only spaces counts as empty.
Make a class in a file named HelloBehavior.quorum:
use Libraries.Interface.Forms.FormBehavior
use Libraries.Interface.Forms.Page
use Libraries.Interface.Events.BehaviorEvent
use Libraries.Interface.Controls.TextField
use Libraries.Game.Graphics.Label
class HelloBehavior is FormBehavior
action Run(BehaviorEvent event)
Page page = GetPage()
TextField field = page:GetTextField("Your first name")
Label greeting = page:GetLabel("Greeting")
if field = undefined or greeting = undefined
return now
end
text name = field:GetText():Trim()
if name = ""
greeting:SetText("Please type your first name first.")
else
greeting:SetText("Hello, " + name + "!")
field:Empty()
end
end
end
When the name is good, the program shows the greeting and empties the field
with Empty, ready for the next name. When it isn't, it explains what to fix.
A good error message says what went wrong and what to do.
Pressing Enter in a text field doesn't run anything by itself. Give people a button, like "Say hello," to send what they typed.
Where screen reader users find the answer
The greeting appears in a label. On a page, labels are in the Tab order, so a screen reader user can press Tab from the button to reach the label and hear the answer. Keep the answer close to the button, so it's easy to find.
What to do when the input isn't right
Typed text often has a data quality problem: extra spaces, a missing answer, a word that is too long, or the wrong kind of value. A program has several options. Each one has a cost:
| Option | Example | Good | Bad |
|---|---|---|---|
| Fix it for the person | Trim removes spaces at the start and end | Nothing for the person to do | The program might change what they meant |
| Ask the person to fix it | "Please type your first name first." | The person stays in charge | It takes them another try |
| Warn while they type | A count that says when a nickname is too long | They find out before they press the button | A message that changes on every key can be distracting |
| Say the rule first | A description: "Up to 12 characters." | Fewer mistakes to begin with | Some people won't read it |
HelloBehavior uses two of these: it fixes extra spaces itself, and asks the
person to fix an empty name. Real programs often mix options like this. When
you choose, think about what could go wrong if the program guesses, and how
much work you are asking the person to do.
Rules that leave people out
A checking rule is a small algorithm, and it carries the beliefs of the
person who wrote it. Here is a made-up rule for a "Full name" field. Its
writer believed every name is a first name, a space and a last name, with
only letters. Make a class in a file named NameBehavior.quorum:
use Libraries.Interface.Forms.FormBehavior
use Libraries.Interface.Forms.Page
use Libraries.Interface.Events.BehaviorEvent
use Libraries.Interface.Controls.TextField
use Libraries.Game.Graphics.Label
class NameBehavior is FormBehavior
action Run(BehaviorEvent event)
Page page = GetPage()
TextField field = page:GetTextField("Full name")
Label result = page:GetLabel("Name result")
text name = field:GetText():Trim()
if name:Contains("-")
result:SetText("Names can't have a hyphen.")
elseif name:Contains("'")
result:SetText("Names can't have an apostrophe.")
elseif name:Contains(" ") = false
result:SetText("Please type a first name and a last name.")
else
result:SetText("Thank you, " + name + ".")
end
end
end
Try it on paper with these made-up people: Ana Lopez-Garcia, Liam O'Brien, and Wati, who has only one name. The rule rejects all three, and each is told their own name is wrong. That is bias: the rule works for some groups of people and fails others, even though nobody meant it to.
These aren't rare cases. Real names can contain hyphens and apostrophes, and in parts of Southern India, Malaysia and Indonesia many people have only a given name, with no family name. Advice from the World Wide Web Consortium (W3C), which writes standards for the web, is to allow punctuation such as hyphens and apostrophes in names, and not to require a family name (source).
A rule like this is harder to notice than a crash, because it only fails for some people, and those people may simply leave. Before you add a rule, ask what it protects, who it might reject, and what happens to a person it rejects by mistake. Sometimes the fairest fix is to remove a rule, not improve it.
Reacting while a person types
Sometimes a program should react to every letter, not wait for a button. A listener is an object that a control calls when something happens. A text
field calls each of its text change listeners after every change: typing,
deleting, pasting, or a call to SetText.
This listener counts the letters in a nickname and warns when it's too long.
Make a class in a file named CountListener.quorum:
use Libraries.Interface.Events.TextChangeListener
use Libraries.Interface.Events.TextChangeEvent
use Libraries.Interface.Controls.Control
use Libraries.Interface.Controls.TextField
use Libraries.Game.Graphics.Label
class CountListener is TextChangeListener
Label label = undefined
integer limit = 12
action SetLabel(Label label)
me:label = label
end
action TextChanged(TextChangeEvent event)
Control control = event:GetControl()
if label not= undefined and control is TextField
TextField field = cast(TextField, control)
integer size = field:GetSize()
if size > limit
label:SetText("Characters: " + size + " of " + limit + ". That is too long.")
else
label:SetText("Characters: " + size + " of " + limit)
end
end
end
end
event:GetControl() returns the control that changed. The listener checks
that it's a TextField with is, then uses cast to treat it as one, so it
can call GetSize. Here is a program that uses it:
use Libraries.Interface.Forms.Form
use Libraries.Interface.Forms.Page
use Libraries.Interface.Controls.TextField
use Libraries.Game.Graphics.Label
Form form
Page page = form:GetMainPage()
page:SetTitle("Pick a Nickname")
page:AddLabel("Nickname:")
TextField nickname = page:AddTextField("Nickname")
nickname:SetDescription("Up to 12 characters.")
Label count = page:AddLabel("Character count", "Characters: 0 of 12")
CountListener listener
listener:SetLabel(count)
nickname:AddTextChangeListener(listener)
form:Display()
Notice that the description tells people the limit before they start typing. Don't rely only on a message that appears after they have made a mistake. The count label is right after the field, so a screen reader user can press Tab to check it at any time.
Password fields
For a password, call SetPassword(true). The field draws a star (*) in
place of each letter:
page:AddLabel("Password:")
TextField password = page:AddTextField("Password")
password:SetPassword(true)
GetText still returns the real text, so your program can check it.
Hiding the letters is a security measure, and like every security measure it protects against some things and costs something. The stars protect against a physical risk: a person nearby reading the password off the screen. They do nothing for the password once it is inside your program. That needs other measures, of the cybersecurity kind, such as never putting the password in a label and never saving it as plain text. The stars also cost the user something: a typing mistake is hard to find.
The US National Institute of Standards and Technology (NIST) publishes rules for how services should handle passwords. There are many of them. Among them:
- A password that is the only step in signing in must be at least 15 characters long, and services should allow at least 64.
- Services should accept every character, including spaces, and must not require a mix of kinds of characters, such as a number and a symbol.
- Services must not make people change passwords on a schedule, store password hints that anyone can read, or use security questions such as the name of a first pet.
- Services must check a new password against a list of common or leaked passwords, and say why a password was turned down.
- Services must allow password managers, and should allow pasting a password.
- Services must store passwords in a scrambled form (salted and hashed), never as plain text.
Long passwords with any characters are easy to mistype when every letter is a star. So a common feature of sign-in forms is a Show password option. NIST says services should offer one, displaying the password instead of dots or asterisks while it is typed, so a person in a place where their screen is unlikely to be observed can confirm what they typed. It also allows a device to show each character for a moment after it is typed, which is common on phones (NIST SP 800-63B, Digital Identity Guidelines: Authentication and Authenticator Management).
Engage
In this activity, you and a partner each build a sign-up form, then test each other's. The goal is to read text from text fields, choose how to handle input that breaks a rule, and find out whether your rules treat people fairly. The form has a "Username" field and a "Full name" field. The username rules are:
- the username isn't empty;
- it has no more than 15 characters;
- it has no spaces. As a hint,
name:Contains(" ")istrueifnamehas a space.
For the full name, start from the rule in NameBehavior. Add a "Check"
button that shows, in a label, either that everything is good or which rule
is broken and how to fix it. Put the username rules in the field's
description, so people know them before they type. For each rule, decide
which option from "What to do when the input isn't right" you will use, and
write your choice and your reason in a comment.
Next, write a test plan for your own form: at least six usernames, with what the program should say for each. Include tricky cases, such as exactly 15 characters, only spaces, and a space at the end. Run every test and fix any that fail.
Then trade with your partner. Without reading each other's code, write at least five full names to try on your partner's form, chosen to find people the rule might reject: different punctuation, one-word names, very short or very long names. You can make them up. Your partner runs them and writes down which were rejected. Back on your own form, change the name rule so it stops rejecting real kinds of names, and ask your partner to run the same names again. In a comment, write who the old rule left out, and what your new rule still checks, and why.
Finally, add a "Password" field with SetPassword(true). With your partner,
list one thing the stars protect against, one thing they don't, and one
thing they make harder for the user.
Wrap up
A text field holds one line of text. You read it with GetText, check it
before you use it, and can react to every change with a listener. Think about
the warm up: which error messages in your program would help a person the
most, and which could be clearer?