HTML 表单非空验证:如何使用 required 属性
在 input 标签中添加 'required' 属性即可实现非空验证。
例如:
<label for='username'>用户名:</label>
<input type='text' id='username' name='username' required>
当用户提交表单时,如果该输入框为空,浏览器会提示用户该字段不能为空。同时,如果使用 JavaScript 进行表单提交,也可以通过验证 input 的 validity 属性来判断该输入框是否为空。
原文地址: https://www.cveoy.top/t/topic/mBt9 著作权归作者所有。请勿转载和采集!