Input Text pattern Property - Set the pattern of a text field to only contain numbers from 1 to 3: - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:Input Text

Description

Input Text pattern Property - Set the pattern of a text field to only contain numbers from 1 to 3:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<form action="#">
  Zip code: <input type="text" id="myText" name="zip_code">
  <input type="submit">
</form>/*from w w  w  .  j av a2  s .  co m*/

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

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

<script>
function myFunction() {
    document.getElementById("myText").pattern = "[1-3]";
    document.getElementById("myText").title = "A number from 1 to 3";
    document.getElementById("demo").innerHTML = "The text field can now only contain numbers from 1 to 3.";
}
</script>

</body>
</html>

Related Tutorials