HTML <textarea> required Attribute

HTMLWeb DevelopmentFront End Technology

The required attribute of the <textarea> element in HTML is used to let visitors know that this textarea field is to be filled before submitting the form. If a visitor clicks on Submit, without filling the textarea set with required attribute, then the form won’t submit.

Following is the syntax −

<textarea required>

Let us now see an example to implement the required attribute of the <textarea> element −

Example

 Live Demo

<!DOCTYPE html>
<html>
<body>
<h2>Subject-wise rank</h2>
<form action="">
   <label for="stinfo">Student:</label>
   <input type="text" id ="stinfo"><br>
   <label for="sub">Subject:</label>
   <input type="text"><br>
   <label for="rank">Rank:</label>
   <input type="number"><br>
   <label for="marks">Marks:</label>
   <input type="number"><br>
   <label for="remark">Remarks</label><br>
   <textarea rows="6" cols="70" required></textarea><br>
   <input type="submit">
</form>
</body>
</html>

Output

In the above example, we have a form with fields and a button −

<form action="">
   <label for="stinfo">Student:</label>
   <input type="text" id ="stinfo"><br>
   <label for="sub">Subject:</label>
   <input type="text"><br>
   <label for="rank">Rank:</label>
   <input type="number"><br>
   <label for="marks">Marks:</label>
   <input type="number"><br>
   <label for="remark">Remarks</label><br>
   <textarea rows="6" cols="70" required></textarea><br>
   <input type="submit">
</form>

We have set a textarea and set it is as required, therefore if the form is submitted without filling the textarea, then a warning message would be visible. This won’t even submit the form if the textarea is empty −

<textarea rows="6" cols="70" required></textarea>
raja
Published on 21-Jun-2019 12:51:24
Advertisements