Live-Eingabevalidierung
Reagieren Sie auf Eingaben und zeigen Sie Validierungshinweise an.
Live-Eingabevalidierung ist eine kostenlose SwiftUI Academy-Lektion auf CoddyKit. Dies ist Lektion 4 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des SwiftUI Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der SwiftUI Academy-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
Guiding the User
Great forms tell users about mistakes as they type, not after they submit. This is live validation, and SwiftUI makes it natural. ✅
State Drives Everything
Validation reads the same @State string the field writes to. As the value changes, you simply recompute whether it is valid.
@State private var email = ""A Computed Check
Add a computed property that returns true when the input is valid. SwiftUI re-reads it on every keystroke, so it always stays current.
var isValid: Bool {
email.contains("@")
}Showing a Hint
Use an if in the body to reveal a message only when the input is invalid. When the value becomes valid, the hint disappears on its own.
if !isValid {
Text("Enter a valid email")
}Color as Feedback
Color speaks faster than words. Tint the hint with foregroundStyle in red so the user instantly notices something needs fixing.
Text("Enter a valid email")
.foregroundStyle(.red)Checking Length
Many rules are about size. Use the count of the string to require a minimum length, like a password of at least eight characters.
var longEnough: Bool {
password.count >= 8
}Trimming Whitespace
Users add stray spaces. Trim them before checking so an entry of only spaces does not slip through as valid by mistake.
let clean = name.trimmingCharacters(
in: .whitespaces)Guarding the Button
Tie the submit Button to your check with the disabled modifier. While the input is invalid, the button simply cannot be tapped.
Button("Save") { save() }
.disabled(!isValid)Reacting to Changes
Need to run code on each edit? The onChange modifier fires whenever the value changes, perfect for logging or extra checks.
TextField("Email", text: $email)
.onChange(of: email) { check() }It All Just Updates
You never manually refresh anything. Because validation reads state, every keystroke recomputes the hints and button for free.
Kind, Clear Feedback
Live validation turns frustration into guidance. Clear hints and a smart submit button make your form feel helpful, not strict. ✨
Quick Check
How do you stop the submit button from being tapped while input is invalid?
Recap
You built live validation from a computed check, showed colored hints, trimmed input, and disabled the button until everything was valid. 🎉
Häufig gestellte Fragen
Ist die Lektion „Live-Eingabevalidierung“ kostenlos?
Ja — der vollständige Text von „Live-Eingabevalidierung“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des SwiftUI Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der SwiftUI Academy-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Live-Eingabevalidierung“?
Reagieren Sie auf Eingaben und zeigen Sie Validierungshinweise an. Du übst SwiftUI Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um SwiftUI Academy zu starten?
Keine Vorkenntnisse erforderlich. SwiftUI Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 4 von 4.
Wie lange dauert die Lektion „Live-Eingabevalidierung“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser SwiftUI Academy-Lektion Code schreiben und ausführen?
Ja. Jede SwiftUI Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- Ein TextField an State binden
- Tastaturtypen und Platzhalter
- SecureField für Passwörter
- Live-Eingabevalidierung