Javascript DOM HTML Input URL pattern Property set

Introduction

Set the pattern of a URL field to only accept URL's that start with http(s)://www.java2s.com/:

Click the button to specify a regular expression that the URL field's value will be checked against. Note that we also set the "title" to describe the pattern.

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

View in separate window

<!DOCTYPE html>
<html>
<body>
<form action="/action_page.php">
  demo2s profile: <input type="url" id="myURL" name="fb">
  <input type="submit">
</form>/*from  www . j  a  v a 2 s .c  o m*/
<button onclick="myFunction()">Test</button>
<p id="demo"></p>
<script>
function myFunction() {
  document.getElementById("myURL").pattern =
  "http://www\.demo2s\.com\/(.+)|https://www\.demo2s\.com\/(.+)";
  document.getElementById("myURL").title =
  "URL must start with http://www.java2s.com/";

  document.getElementById("demo").innerHTML =
  "The URL field now only accept URL's that start with http://www.java2s.com/ or https://www.java2s.com/.";
}
</script>

</body>
</html>



PreviousNext

Related