The required attribute in an HTML <input> element is used to specify that the user must fill out the field before submitting a form. It ensures that the form is not submitted if the field is empty. This attribute is often used for fields like email, password, or any other input where user input is mandatory.
When a form is submitted, the browser will automatically check if the field is filled. If it isn't, the form won't submit, and the user will see a validation message.
required-attribute.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Required Attribute Example</title> </head> <body> <form action="https://httpbin.org/post" method="POST"> <label for="name">Name:</label> <input type="text" id="name" name="name" required> <br><br> <label for="email">Email:</label> <input type="email" id="email" name="email" required> <br><br> <input type="submit" value="Submit"> </form> </body> </html>
Above snippet generate below screen.
Just click on Submit button without entering any data, you can see the validation message against Name field.
Previous Next Home
No comments:
Post a Comment