A form that checks what's typed
Build a sign-up form whose fields say what's wrong as people type, and whose button checks again before going on.
What you'll build: a sign-up form with a name, an email address and a phone number. The email and phone fields say what's wrong while people type. Sign up, or Return in the last field, checks the form, puts the cursor where something's missing, and says thanks when it's all there.
What you'll learn:
- Give a Text Field rules with Validation, and the keyboard that goes with them.
- Check a form in steps, with If, Show Message and Stop.
- Move the cursor to a field with Focus Field.
- Run the same steps from a button and from the Return key.
#The variables
| Name | Type | Starting value |
|---|---|---|
fullName | Text | Empty |
email | Text | Empty |
phone | Text | Empty |
Make them in Data & Logic with Add variable. Each is named after its type, so tap each name and type the one from the table.
#Steps
#The fields
- Add three Text Fields. Set their Titles to
Name,EmailandPhone, and pickfullName,emailandphonein their Text menus. - Select the Email field. Under Validation, set Must be to Email.
- Select the Phone field and set Must be to Phone number. To say it your own way, type into Message, like
Use digits, like 555 0100. Left empty, the app uses its own words. - Rename the fields so steps can find them: in Layers, select each field, open the ⋯ menu on its row and choose Rename (or double-tap the row). Call them
Name field,Email fieldandPhone field.
Must be offers these rules. Each also picks the keyboard, unless the field has a Keyboard Type modifier of its own.
| Must be | Keyboard | Message when the text doesn't fit |
|---|---|---|
| Anything | The field's own | None |
| Use an email address, like name@example.com. | ||
| Phone number | Phone pad | Use a phone number: digits, spaces, + ( ) and -. |
| Number | Numbers, with a decimal point | Use a number, like 12 or 3.5. |
| Whole number | Numbers | Use a whole number, like 12. |
| Web address | Web address | Use a web address that starts with https://. |
| Custom pattern | Standard | That doesn't match the pattern. |
Custom pattern adds a Pattern box for a regular expression the whole text must match, like [A-Z]{2}[0-9]{4}.
#The checks
The button and the Return key both check the form, so the steps go in a reusable action.
- In Data & Logic, under Reusable actions, tap New action. Add these steps:
| Step | Settings |
|---|---|
| If | Condition isEmpty(trim(fullName)). Under Then: Show Message (Text Add your name, Style Warning), Focus Field (Id Name field), Stop. |
| If | Condition !contains(email, '@'). Under Then: Show Message (Text Check your email address, Style Warning), Focus Field (Id Email field), Stop. |
| Show Message | Text Thanks, {fullName}!, Style Success |
- Tap Done, then tap the action's name,
newAction, and change it tosignUp.
#Run them
- Add a Button titled
Sign up. In its Actions tab, add Run Action and picksignUpunder Action. - Select the Phone field. In its Actions tab, tap the When card and choose When Return Is Pressed. Add Run Action with
signUphere too.
Tap Preview. Type maya@ in the Email field: the message shows under it. Tap Sign up with the name empty: a message asks for it, and the cursor jumps to the Name field.
#How it works
- Validation looks at the text each time it changes and writes what's wrong under the field, in red. An empty field shows no message: it only judges what's been typed.
- Validation doesn't stop anything. A button or a Return key still runs its steps while a field shows a message. That's why the steps check again before going on.
- The checks:
isEmpty(trim(fullName))is true when the name is empty or only spaces. Formulas can't run the Email rule itself, so!contains(email, '@')is a rough stand-in; Invoice Maker makes the same check before it sends an invoice. - Stop ends the run. The thank-you only shows when no If stopped it.
- Focus Field finds a field by its name in Layers (or its id), so the fields need names you chose.