Week 4 Post-class 1.2: Test your Knowledge
这一页有部分内容暂时无法显示,其余内容完整。
Question 1
Why is form validation considered essential in modern web applications, particularly from a security and user experience perspective?
- It primarily enhances the aesthetic design of the web application.
- It ensures that user data is accurately captured, securely processed, and correctly formatted, improving security and user experience.
- It allows users to bypass security measures for quicker form submission.
- It reduces the need for server-side scripting by enabling more user flexibility.
为什么表单验证在现代 Web 应用中至关重要,尤其从安全与用户体验角度?
此处有一段内容暂不支持显示。
- 1 错误:验证关注数据正确性与安全,不是“美观”。
- 2 正确:核心目标是数据完整性、格式正确与安全处理,直接提升 UX 与安全。
- 3 错误:验证的目的恰恰是阻止绕过安全。
- 4 错误:即使有前端验证,也仍需要服务端验证与逻辑处理。
Question 2
What is the key reason server-side validation is often considered more secure and reliable than client-side validation?
- It allows for immediate feedback to the user without the need to reload the page.
- It reduces the server load by validating data before it is sent to the server.
- It can access databases and perform complex validations while ensuring consistent behavior across all browsers and devices.
- It eliminates the need for any client-side validation, making the development process simpler.
为什么服务器端验证通常被认为比客户端验证更安全、更可靠?
此处有一段内容暂不支持显示。
- 1 错误:即时反馈是客户端验证的优势。
- 2 错误:此说法更贴近前端预验证;服务端验证不以“减负载”为主要理由。
- 3 正确:服务端能连库查重、权限、业务规则,且不受浏览器差异与用户篡改影响。
- 4 错误:两者互补,不能以服务端验证为由取消前端验证。
Question 3
Which of the following is a drawback of client-side form validation?
- It increases server load.
- It cannot be bypassed by users.
- It may not work consistently across all browsers.
- It is less secure because users can disable JavaScript or manipulate the DOM.
下列哪项是客户端表单验证的缺点?
此处有一段内容暂不支持显示。
- 1 错误:前端验证通常减少无效请求,反而有助于减轻负载。
- 2 错误:客户端逻辑可被禁用/绕过,并不可靠。
- 3 不作为正确项:历史上可能存在兼容性问题,但现代标准已缓解;关键缺点在于安全可绕过。
- 4 正确:只在浏览器执行,易被禁用或改写,因此必须配合服务端验证兜底。
Question 4
In HTML built-in form validation, how does the pattern attribute enhance the validation process, and what is a common use case for it?
- It sets a regular expression that the input data must match, often used for validating phone numbers, email addresses, or specific formats.
- It restricts the number of characters a user can input in a text field, commonly used for short text areas.
- It forces users to select a value from a predefined set of options, typically used in drop-down menus.
- It defines the maximum and minimum number of input fields a user can submit, used in dynamic forms.
在 HTML 内置验证中,pattern 属性如何增强验证?常见用例是什么?
此处有一段内容暂不支持显示。
- 1 正确:
pattern用正则表达式描述格式要求,原生校验不通过则阻止提交。 - 2 错误:限制长度应使用
maxlength/minlength。 - 3 错误:这是
<select>/datalist的职责,非pattern。 - 4 错误:与字段数量无关。
Question 5
Based on the pre-reading slide 5, which method is used in JavaScript to prevent form submission if the validation fails?
stopPropagation()validate()checkValidity()event.preventDefault()
根据预读幻灯片第 5 页,若校验失败,JavaScript 使用哪个方法阻止表单提交?
此处有一段内容暂不支持显示。
- 1 错误:只阻止事件冒泡,不会取消默认提交。
- 2 错误:非原生标准 API,常见于第三方库。
- 3 易混淆:它用于触发/检查原生校验并返回布尔值,但不会自动阻止提交;通常写法是:
if (!form.checkValidity()) event.preventDefault(); - 4 正确:调用该方法可取消提交这一“默认行为”。