Javascript DOM HTML Input Text pattern Property get

Introduction

Get the value of the pattern attribute of a text field:

var x = document.getElementById("myText").pattern;

The following text field can contain only three letters:

Click the button to display the value of the pattern attribute of the text field.

View in separate window

<!DOCTYPE html>
<html>
<body>
<form action="/action_page.php">
  Country code: /*from  ww w  . j a  va  2 s  .  c  om*/
  <input type="text" 
         id="myText" 
         name="country_code" 
         pattern="[A-Za-z]{3}" 
         title="Three letter country code">
  <input type="submit">
</form>
<button onclick="myFunction()">Test</button>

<p id="demo"></p>

<script>
function myFunction() {
  var x = document.getElementById("myText").pattern;
  document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

The pattern property sets or gets the pattern attribute of a text field.

The pattern attribute uses a regular expression to validate text field's value.

The pattern property accepts and returns a String type value.

Value Description
regexp Specifies a regular expression that the text field's value is checked against
Return Value A String, representing a regular expression



PreviousNext

Related