Javascript DOM HTML Input URL pattern Property get

Introduction

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

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

The following pattern requires that the URL value must start with http:// or https:// followed by at least one character:

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

elements with type="url" are not supported in Safari.

View in separate window

<!DOCTYPE html>
<html>
<body>

<form action="/action_page.php">
  Homepage: <input type="url" id="myURL" name="website" pattern="https?://.+" title="Include http://">
  <input type="submit">
</form>/*from   w w w.  j a  v a 2s  .  com*/
<button onclick="myFunction()">Test</button>
<p id="demo"></p>

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

</body>
</html>

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

The pattern attribute sets a regular expression to validate URL field's value.

Value Description
regexp a regular expression

The pattern property returns a String representing a regular expression.




PreviousNext

Related