Lesson 69: Presence and format checks
หน้านี้ต้องใช้เบราว์เซอร์ล่าสุด (รองรับ SharedArrayBuffer) กรุณาอัปเดต Chrome, Edge, Firefox หรือ Safari เป็นเวอร์ชันล่าสุด
English
Two more validation checks
- Lesson 40 had the range, length and type checks. Here are two more.
- A presence check: something was entered (the field is not empty).
- A format check: the data has the right pattern, for example 2 letters then 4 digits.
| Check | Example | What it rejects |
|---|---|---|
| presence | a name must be entered | an empty line |
| format | ID like AB1234 | A12345, ab1234, AB12C4 |
| length | ID has 6 characters | AB123 |
| check digit | the last digit is worked out from the others (lesson 41) | a mistyped code |
English
In the exam
- 0478/22/F/M/25 Q4(a): state the purpose of a presence check. Mark scheme: to check that a value has been entered.
- 0478/23/M/J/23 Q5(a): link each description to a check. Data is an integer: type check; some data has been entered: presence check; the right number of characters: length check; an identification number contains no errors: check digit. (The format check is not used there.)
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
A presence check makes sure that something was entered. Type a name in the Serial Monitor; if the line is empty, print Error: enter a name instead of booking. Check sends an empty line first, then Ana.
คลิก Run เพื่อดูผลลัพธ์ที่นี่
ความยาก 4/5 · 280 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
A format check makes sure the data has the right pattern. A student ID is 2 capital letters and then 4 digits, like AB1234. Finish validId: return false if the length is not 6, if one of the first 2 characters is not a capital letter, or if one of the last 4 is not a digit.
คลิก Run เพื่อดูผลลัพธ์ที่นี่