質問するログイン新規登録

回答編集履歴

2

`pattern` 属性から `\^\$` を削除。`blur, focusout` イベントのコード追加。

2016/03/30 17:31

投稿

think49
think49

スコア18194

answer CHANGED
@@ -1,7 +1,6 @@
1
- 未検証ですが、`input.checkValidity()` で出来そうな気がしま
1
+ `input.reportValidity()` で期待通りの動作しました
2
- **(2016/03/31 01:40追記)** 訂正。`input.reportValidity()` で期待通りの動作しました。
3
2
 
4
- - [input[type=text]のvalidate - JSFiddle](https://jsfiddle.net/mchw17L4/1/)
3
+ - [input[type=text]のvalidate - JSFiddle](https://jsfiddle.net/mchw17L4/2/)
5
4
  - [4.10.5 input要素 — HTML 5.1 日本語訳](http://momdo.github.io/html51/forms.html#the-input-element)
6
5
  - [Forms in HTML5 - HTML | MDN](https://developer.mozilla.org/ja/docs/Web/HTML/Forms_in_HTML#Constraint_Validation_API)
7
6
 
@@ -11,8 +10,28 @@
11
10
  function handleInput (event) {
12
11
  event.target.reportValidity();
13
12
  }
13
+
14
+ function handleBlur (event) {
15
+ var input = event.target;
16
+
17
+ setTimeout(input.reportValidity.bind(input), 0)
18
+ }
19
+
20
+ document.addEventListener('focusout', function handleFocusout (event) {
21
+ var input = event.target;
22
+
23
+ if (input.id === 'sample') {
24
+ input.reportValidity();
25
+ }
26
+ }, false);
14
27
  </script>
15
- <input type="text" pattern="^[^\u0000-\u007F]*$" oninput="handleInput(event);">
28
+ <label>input <input type="text" pattern="[^\u0000-\u007F]*" oninput="handleInput(event);"></label>
29
+ <label>blur <input type="text" pattern="[^\u0000-\u007F]*" onblur="handleBlur(event);"></label>
30
+ <label>focusout <input id="sample" type="text" pattern="[^\u0000-\u007F]*"></label>
16
31
  ```
17
32
 
33
+ **(更新履歴)**
34
+ - 2016/03/31 01:40 `input.checkValidity()` を `input.reportValidity()` に修正
35
+ - 2016/03/31 02:30 `pattern` 属性から `^$` を削除。`blur, focusout` イベントのコード追加。
36
+
18
37
  Re: k499778 さん

1

コード追加 input\.reportValidity\(\);

2016/03/30 17:31

投稿

think49
think49

スコア18194

answer CHANGED
@@ -1,6 +1,18 @@
1
1
  未検証ですが、`input.checkValidity()` で出来そうな気がします。
2
+ **(2016/03/31 01:40追記)** 訂正。`input.reportValidity()` で期待通りの動作しました。
2
3
 
4
+ - [input[type=text]のvalidate - JSFiddle](https://jsfiddle.net/mchw17L4/1/)
3
5
  - [4.10.5 input要素 — HTML 5.1 日本語訳](http://momdo.github.io/html51/forms.html#the-input-element)
4
6
  - [Forms in HTML5 - HTML | MDN](https://developer.mozilla.org/ja/docs/Web/HTML/Forms_in_HTML#Constraint_Validation_API)
5
7
 
8
+ ```HTML
9
+ <script>
10
+ 'use strict';
11
+ function handleInput (event) {
12
+ event.target.reportValidity();
13
+ }
14
+ </script>
15
+ <input type="text" pattern="^[^\u0000-\u007F]*$" oninput="handleInput(event);">
16
+ ```
17
+
6
18
  Re: k499778 さん