0Pricing
SwiftUI Academy · درس

التحقق المباشر من الإدخال

استجب للكتابة واعرض ملاحظات التحقق

التحقق المباشر من الإدخال درس مجاني في SwiftUI Academy على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في SwiftUI Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة SwiftUI Academy 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

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. 🎉

الأسئلة الشائعة

هل درس «التحقق المباشر من الإدخال» مجاني؟

نعم — نص درس «التحقق المباشر من الإدخال» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة SwiftUI Academy، انتقل إلى CoddyKit PRO. تتضمن دورة SwiftUI Academy 4 دروس في المجموع.

ماذا ستتعلم في «التحقق المباشر من الإدخال»؟

استجب للكتابة واعرض ملاحظات التحقق تتمرن على SwiftUI Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ SwiftUI Academy؟

لا تُشترط خبرة سابقة. SwiftUI Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.

كم من الوقت يستغرق درس «التحقق المباشر من الإدخال»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس SwiftUI Academy هذا؟

نعم. كل درس في SwiftUI Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. ربط TextField بالحالة
  2. أنواع لوحة المفاتيح والعناصر النائبة
  3. SecureField لكلمات المرور
  4. التحقق المباشر من الإدخال
← العودة إلى SwiftUI Academy