简体   繁体   English

需要输入HTML5

[英]Input Required HTML5

I have this form in html5: 我在html5中有此表单:

 <form method="post" action="form.php" >
    <input id="fname" name="fname" type="text" value=""  required/>
    <input id="lname" name="lname" type="text" value=""  required/>
    <input id="submit" name="submit" type="submit" value="" />
 </form>

Sometimes i receive empty fields. 有时我会收到空​​白字段。 Can you tell me why? 你能告诉我为什么吗?

It is best practice to include the aria-required attribute too: 最好的做法是也包含aria-required属性:

<form method="post" action="form.php" >
        <input id="fname" name="fname" type="text" value=""  required aria-required=”true” />
        <input id="lname" name="lname" type="text" value=""  required aria-required=”true” />
</form>

Note that older browsers may not support support these attributes. 请注意,较早的浏览器可能不支持支持这些属性。 On any browser that doesn't support these attributes, front-end form validation could still be achieved through javascript. 在任何不支持这些属性的浏览器上,仍然可以通过javascript来实现前端表单验证。 You can learn more about javascript form validation here . 您可以在此处了解有关javascript表单验证的更多信息。

Remember it is important to validate input on the server too. 请记住,验证服务器上的输入也很重要。

You can't make yours constraints only on the client side. 您不能仅在客户端进行约束。 Anyone can send a POST request to your server without using your form easily. 任何人都可以轻松地向您的服务器发送POST请求,而无需使用您的表单。
It's possible with all recent browsers to edit the DOM and remove the required part too. 所有最新的浏览器都可以编辑DOM并删除required部分。

HTML5 Validations are great for helping users but it's not for security/checking. HTML5验证非常适合帮助用户,但不适用于安全性/检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM