Input Search pattern Property - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:Input Search

Description

The pattern property sets or gets the pattern attribute of a search field, which sets a regular expression that the search field's value is checked against.

Set the pattern property with the following Values

Value Description
regexp Sets a regular expression for the text field's value

Return Value

A String, representing a regular expression

The following code shows how to get the value of the pattern attribute of a search field:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<p>A form with a search field that CANNOT contain the following characters: ' or "</p>

<form action="#">
  Search: <input type="search" id="mySearch" name="search" pattern="[^'\x22]+" title="Invalid input">
  <input type="submit">
</form>/*  w  w w .ja  va 2  s.  c o m*/

<button onclick="myFunction()">Test</button>

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

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

</body>
</html>

Related Tutorials