aboutsummaryrefslogtreecommitdiff
path: root/files/ru/web/html
diff options
context:
space:
mode:
authorPeter Bengtsson <mail@peterbe.com>2020-12-11 19:00:14 -0500
committerPeter Bengtsson <mail@peterbe.com>2020-12-11 19:00:14 -0500
commitba5d6f9610d6bb352eecfa3ded1bb99bc9892916 (patch)
tree4ca1fd3d01433b96fce40c473a3b6b272be393eb /files/ru/web/html
parentd192fb918b0e2aa8869de6dcc59de8464b6e879a (diff)
downloadtranslated-content-ba5d6f9610d6bb352eecfa3ded1bb99bc9892916.tar.gz
translated-content-ba5d6f9610d6bb352eecfa3ded1bb99bc9892916.tar.bz2
translated-content-ba5d6f9610d6bb352eecfa3ded1bb99bc9892916.zip
dump 2020-12-11
Diffstat (limited to 'files/ru/web/html')
-rw-r--r--files/ru/web/html/attributes/pattern/index.html157
1 files changed, 157 insertions, 0 deletions
diff --git a/files/ru/web/html/attributes/pattern/index.html b/files/ru/web/html/attributes/pattern/index.html
new file mode 100644
index 0000000000..b7df7361ed
--- /dev/null
+++ b/files/ru/web/html/attributes/pattern/index.html
@@ -0,0 +1,157 @@
+---
+title: 'HTML attribute: pattern'
+slug: Web/HTML/Attributes/pattern
+translation_of: Web/HTML/Attributes/pattern
+---
+<p>{{HTMLSidebar}}</p>
+
+<p>Атрибут <strong><code>pattern</code></strong> определяет <a href="/ru/docs/Web/JavaScript/Guide/Regular_Expressions">регулярное выражение</a>, которому должно соответствовать значение элемента формы. Если  ненулевое значение не соответствует ограничениям, установленным в <code>pattern</code>, доступное только для чтения свойство {{domxref('ValidityState.patternMismatch','patternMismatch')}} объекта {{domxref('ValidityState')}} будет истинным.</p>
+
+<p>Атрибут <code>pattern</code> является атрибутом для полей ввода с типом {{HTMLElement("input/text", "text")}}, {{HTMLElement("input/tel", "tel")}}, {{HTMLElement("input/email", "email")}}, {{HTMLElement("input/url", "url")}}, {{HTMLElement("input/password", "password")}}, и {{HTMLElement("input/search", "search")}}.</p>
+
+<div id="pattern-include">
+<p>The <code>pattern</code> attribute, when specified, is a regular expression which the input's {{htmlattrxref("value")}} must match in order for the value to pass <a href="/en-US/docs/Web/Guide/HTML/HTML5/Constraint_validation">constraint validation</a>. It must be a valid JavaScript regular expression, as used by the {{jsxref("RegExp")}} type, and as documented in our <a href="/en-US/docs/Web/JavaScript/Guide/Regular_Expressions">guide on regular expressions</a>; the <code>'u'</code> flag is specified when compiling the regular expression, so that the pattern is treated as a sequence of Unicode code points, instead of as ASCII. No forward slashes should be specified around the pattern text.</p>
+
+<p>If the specified pattern is not specified or is invalid, no regular expression is applied and this attribute is ignored.</p>
+
+<div class="note">
+<p><strong>Tip:</strong> Use the {{htmlattrxref("title", "input")}} attribute to specify text that most browsers will display as a tooltip to explain what the requirements are to match the pattern. You <strong>must not</strong> rely on the tooltip alone for an explanation. See below for more information on usability.</p>
+</div>
+</div>
+
+<p>Some of the input types supporting the pattern attribute, notably the {{HTMLElement("input/email", "email")}} and {{HTMLElement("input/url", "url")}} input types, have expected value syntaxes that must be matched. If the pattern attribute isn't present, and the value doesn't match the expected syntax for that value type, the {{domxref('ValidityState')}} object's read-only {{domxref('ValidityState.typeMismatch','typeMismatch')}} property will be true.</p>
+
+<h3 id="Usability">Usability</h3>
+
+<p>When including a <code>pattern</code>, provide a description of the pattern in visible text near the control. Additionally, include a <code><a href="/en-US/docs/Web/HTML/Global_attributes/title">title</a></code> attribute which gives a description of the pattern. User agents may use the title contents during constraint validation to tell the user that the pattern is not matched. Some browsers show a tooltip with title contents, improving usability for sighted users. Additionally, assistive technology may read the title aloud when the control gains focus, but this should not be relied upon for accessibility.</p>
+
+<h3 id="Constraint_validation">Constraint validation</h3>
+
+<p>If the input’s value is not the empty string and the value does not match the entire regular expression, there is a from a {{domxref('ValidityState.patternMismatch','patternMismatch')}}.<br>
+ The pattern's regular expression, when matched against the value, must have its start anchored to the start of the string and its end anchored to the end of the string, which is slightly different from JavaScript regular expressions: in the case of pattern attribute, we are matching against the entire value, not just any subset, as if a <code>^(?:</code> were implied at the start of the pattern and <code>)$</code> at the end.</p>
+
+<h2 id="Examples">Examples</h2>
+
+<p>Given the following:</p>
+
+<div id="example1">
+<pre class="brush: html notranslate">&lt;p&gt;
+ &lt;label&gt;Enter your phone number in the format (123)456-7890
+ (&lt;input name="tel1" type="tel" pattern="[0-9]{3}" placeholder="###" aria-label="3-digit area code" size="2"/&gt;)-
+ &lt;input name="tel2" type="tel" pattern="[0-9]{3}" placeholder="###" aria-label="3-digit prefix" size="2"/&gt; -
+ &lt;input name="tel3" type="tel" pattern="[0-9]{4}" placeholder="####" aria-label="4-digit number" size="3"/&gt;
+ &lt;/label&gt;
+&lt;/p&gt;</pre>
+
+<p>Here we have 3 sections for a north American phone number with an implicit label encompassing all three components of the phone number, expecting 3-digits, 3-digits and 4-digits respectively, as defined by the <code><a href="/en-US/docs/Web/HTML/Attributes/pattern">pattern</a></code> attribute set on each.</p>
+
+<p>If the values are too long or too short, or contain characters that aren't digits, the patternMismatch will be true. When <code>true</code>, the element matches the {{cssxref(":invalid")}} CSS pseudo-classes.</p>
+
+<pre class="brush: css notranslate">input:invalid {
+ border: red solid 3px;
+}</pre>
+
+<p>{{EmbedLiveSample("example1", 300, 40)}}</p>
+</div>
+
+<p>Had we used <code><a href="/en-US/docs/Web/HTML/Attributes/minlength">minlength</a></code> and <code><a href="/en-US/docs/Web/HTML/Attributes/maxlength">maxlength</a></code> attributes instead, we may have seen {{domxref('validityState.tooLong')}} or {{domxref('validityState.tooShort')}} being true.</p>
+
+<h3 id="Specifying_a_pattern">Specifying a pattern</h3>
+
+<p>You can use the {{htmlattrxref("pattern","input")}} attribute to specify a regular expression that the inputted value must match in order to be considered valid (see <a href="/en-US/docs/Learn/HTML/Forms/Form_validation#Validating_against_a_regular_expression">Validating against a regular expression</a> for a simple crash course on using regular expressions to validate inputs).</p>
+
+<p>The example below restricts the value to 4-8 characters and requires that it contain only lower-case letters.</p>
+
+<pre class="brush: html notranslate">&lt;form&gt;
+ &lt;div&gt;
+ &lt;label for="uname"&gt;Choose a username: &lt;/label&gt;
+ &lt;input type="text" id="uname" name="name" required size="45"
+ pattern="[a-z]{4,8}" title="4 to 8 lowercase letters"&gt;
+ &lt;span class="validity"&gt;&lt;/span&gt;
+ &lt;p&gt;Usernames must be lowercase and 4-8 characters in length.&lt;/p&gt;
+ &lt;/div&gt;
+ &lt;div&gt;
+ &lt;button&gt;Submit&lt;/button&gt;
+ &lt;/div&gt;
+&lt;/form&gt;</pre>
+
+<div class="hidden">
+<pre class="brush: css notranslate">div {
+ margin-bottom: 10px;
+ position: relative;
+}
+
+p {
+ font-size: 80%;
+ color: #999;
+}
+
+input + span {
+ padding-right: 30px;
+}
+
+input:invalid+span:after {
+ position: absolute;
+ content: '✖';
+ padding-left: 5px;
+}
+
+input:valid+span:after {
+ position: absolute;
+ content: '✓';
+ padding-left: 5px;
+}</pre>
+</div>
+
+<p>This renders like so:</p>
+
+<p>{{ EmbedLiveSample('Specifying_a_pattern', 600, 110) }}</p>
+
+<h3 id="Accessibility_Concerns">Accessibility Concerns</h3>
+
+<p>When a control has a <code>pattern</code> attribute, the <code>title</code> attribute, if used, must describe the pattern. Relying on the <code>title</code> attribute for the visual display of text content is generally discouraged as many user agents do not expose the attribute in an accessible manner. Some browsers show a tooltip when an element with a title is hovered, but that leaves out keyboard-only and touch-only users. This is one of the several reasons you must include information informing users how to fill out the the control to match the requirements.</p>
+
+<p>While <code>title</code>s are used by some browsers to populate error messaging, because browsers sometimes also show the title as text on hover, it therefore shows in non-error situations, so be careful not to word titles as if an error has occurred.</p>
+
+<h2 id="Specifications">Specifications</h2>
+
+<table class="standard-table">
+ <thead>
+ <tr>
+ <th scope="col">Specification</th>
+ <th scope="col">Status</th>
+ <th scope="col">Comment</th>
+ </tr>
+ </thead>
+ <tbody>
+ <tr>
+ <td>{{ SpecName('HTML WHATWG', 'forms.html#attr-input-pattern', 'pattern') }}</td>
+ <td>{{Spec2('HTML WHATWG')}}</td>
+ <td></td>
+ </tr>
+ <tr>
+ <td>{{ SpecName('HTML5.1', 'forms.html#attr-input-pattern', 'pattern') }}</td>
+ <td>{{Spec2('HTML5.1')}}</td>
+ <td></td>
+ </tr>
+ <tr>
+ <td>{{ SpecName('HTML5 W3C', 'forms.html#attr-input-pattern', 'pattern') }}</td>
+ <td>{{Spec2('HTML5 W3C')}}</td>
+ <td></td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="Browser_compatibility">Browser compatibility</h2>
+
+
+
+<p>{{Compat("html.elements.attributes.pattern")}}</p>
+
+<h2 id="See_also">See also</h2>
+
+<ul>
+ <li><a href="/en-US/docs/Web/Guide/HTML/HTML5/Constraint_validation">Constraint validation</a></li>
+ <li><a href="/en-US/docs/Web/Guide/HTML/Forms/Data_form_validation">Forms: Data form validation</a></li>
+ <li><a href="/en-US/docs/Web/JavaScript/Guide/Regular_Expressions">Regular Expressions</a></li>
+</ul>