Input Text pattern Attribute

Definition and Usage

pattern Attribute sets or returns the value of the pattern attribute of the text field.

The HTML pattern attribute specifies a regular expression to check the value of the text field.

Tip:Please use global HTML title attribute or DOM title Use attributes to describe the pattern to help users.

See also:

JavaScript Tutorial:Expresiones regulares de JavaScript

JavaScript Reference Manual:JavaScript RegExp Object

HTML Reference Manual:Atributo pattern del <input> de HTML

Example

Example 1

Get the pattern attribute value of the text field:

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

Try it yourself

Example 2

Set the pattern of the text field to only contain numbers from 1 to 3:

document.getElementById("myText").pattern = "[1-3]";

Try it yourself

Syntax

Return the pattern attribute:

textObject.pattern

Set the pattern attribute:

textObject.pattern = regexp

attribute value

value description
regexp Define the regular expression for checking the value of the text field.

Detalles técnicos

Valor de retorno: Valor de cadena que representa la expresión regular.

Compatibilidad del navegador

Los números en la tabla indican la versión del navegador que admite completamente la propiedad.

Chrome Edge Firefox Safari Opera
Chrome Edge Firefox Safari Opera
Soporte 10.0 Soporte No soportado Soporte