Javascript Reference - HTML DOM Textarea required Property








The required property sets or gets whether a textarea must be filled before submitting a form.

Browser Support

required Yes 10.0 Yes Yes Yes

Syntax

Return the required property.

var v = textareaObject.required 

Set the required property.

textareaObject.required=true|false

Property Values

Value Description
true|false Set whether a textarea is required for form submission.
  • true - The textarea is required
  • false - Default. The textarea is not required




Return Value

A Boolean type value, true if the textarea is a required part of form submission, otherwise false.

Example

The following code shows how to check if a textarea must be filled before submitting a form.


<!DOCTYPE html>
<html>
<body>
<form action="url">
<textarea id="myTextarea" name="adr" required>this is a test</textarea>
</form><!--from   ww  w .j  a v a  2 s.co m-->
<button onclick="myFunction()">test</button>
<p id="demo"></p>
<script>
function myFunction() {
    var x = document.getElementById("myTextarea").required;
    document.getElementById("demo").innerHTML = x;
}
</script>
</body>
</html>

The code above is rendered as follows:





Example 2

The following code shows how to set a textarea to be a required part of form.


<!DOCTYPE html>
<html>
<body>
<form action="url">
<textarea id="myTextarea" name="adr"></textarea>
<input type="submit">
</form><!--from w w w  .  j a  v  a2s. co m-->
<button onclick="myFunction()">test</button>
<script>
function myFunction() {
    document.getElementById("myTextarea").required = true;
}
</script>
</body>
</html>

The code above is rendered as follows: