Form validation
Validating on the client for comfort and on the server for security, validation rules, sending the form back with its errors and values, and redirecting after a successful submission.
- 24 cards
- 5 topics
- Free
The outline, topic by topic
Each topic is weighted the way the exam weighs it.
- Concepts
- Questions
- Sort and link
- Scenarios
- Understand
A few cards from the deck
Server-side validation: what is it?
Show the answer
A check of the data received, made by the application before using or storing it, whatever tool sent it.
The server cannot trust the browser: a script or
curlsends whatever it wants. It is the only check that really protects the data.The server validates a postcode with this pattern. Which input does it wrongly accept?
```javascript
/[0-9]{5}/.test(saisie)`ab75011cd7501750 117501A
Show the answer
ab75011cdWithout
^or$, the pattern looks for five consecutive digits anywhere in the text, and75011is in there. The other three inputs never have five digits in a row. The right pattern is/^[0-9]{5}$/.The browser checks a field thanks to HTML5 attributes:
required,type='email',min,maxand ….Show the answer
The browser checks a field thanks to HTML5 attributes:
required,type='email',min,maxand pattern.The field
<input name='cp' pattern='[0-9]{5}'>blocks submission if the input is not made of five digits. The browser anchors the pattern by itself.Client-side validation: what is it?
Show the answer
A check made in the browser, by HTML5 attributes or JavaScript, before the form is even sent.
It gives an immediate answer and saves a round trip. But the user can get around it, so it never replaces the server's check.
An order with a negative price reaches the database, although the HTML field has
min='0'. What is the cause and the fix?requiredwas missing: the browser ignoresminwithout it- The database accepts anything; the column type must be changed
- The browser misbehaved; it needs updating
- The form was bypassed; revalidate on the server
Show the answer
The form was bypassed; revalidate on the server
minonly acts in the browser, when the user clicks the submit button, and needs norequiredonce a value is entered. A script orcurlwrites thePOSTrequest itself, with no check at all. The server must refuse any negative price.