简体   繁体   中英

Input Required HTML5

I have this form in 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:

<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. You can learn more about javascript form validation here .

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.
It's possible with all recent browsers to edit the DOM and remove the required part too.

HTML5 Validations are great for helping users but it's not for security/checking.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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