質問編集履歴
3
他のテキストボックスに影響が出ないよう自身のerrorのみ判定するように修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -81,7 +81,7 @@
|
|
81
81
|
});
|
82
82
|
// 制限文字数チェック実行
|
83
83
|
$('.txb').on('blur', function() {
|
84
|
-
limitCheck();
|
84
|
+
limitCheck($(this));
|
85
85
|
});
|
86
86
|
$('.btn').on('click', function() {
|
87
87
|
limitCheck();
|
2
文字数オーバーerror判定用のclass付与位置を変更
title
CHANGED
File without changes
|
body
CHANGED
@@ -62,16 +62,16 @@
|
|
62
62
|
}
|
63
63
|
// 入力文字数表示
|
64
64
|
function cntChar(elm) {
|
65
|
-
|
65
|
+
'use strict';
|
66
|
-
|
66
|
+
var p = elm.parents('.hoge');
|
67
|
-
|
67
|
+
var cnt = elm.val().length;
|
68
|
-
|
68
|
+
var limit = parseInt(p.find('.limit').text(), 10);
|
69
|
-
|
69
|
+
p.find('.num').text(cnt);
|
70
|
-
|
70
|
+
if (cnt > limit) {
|
71
|
-
|
71
|
+
elm.addClass('error');
|
72
|
-
|
72
|
+
} else {
|
73
|
-
|
73
|
+
elm.removeClass('error');
|
74
|
-
|
74
|
+
}
|
75
75
|
}
|
76
76
|
$(function() {
|
77
77
|
'use strict';
|
1
文字数オーバーしたテキストボックスから別のテキストボックスにfocusを移すと無限にalertが出てしまっていたのでblur時の処理を修正しました
title
CHANGED
File without changes
|
body
CHANGED
@@ -33,29 +33,32 @@
|
|
33
33
|
```
|
34
34
|
|
35
35
|
```javascript
|
36
|
-
// 制限文字数チェック
|
37
|
-
function limitCheck() {
|
36
|
+
function limitCheck(elm) {
|
38
|
-
|
37
|
+
'use strict';
|
39
|
-
|
38
|
+
var msg = '';
|
40
|
-
|
39
|
+
var flg = 0;
|
41
|
-
|
40
|
+
var checkTgt = '.txb';
|
42
41
|
|
42
|
+
if (event.type === 'blur') {
|
43
|
+
if (elm.hasClass('error')) {
|
44
|
+
var limit = parseInt(elm.siblings('.fuga').children('.limit').text(), 10);
|
45
|
+
msg = limit + '文字以内で入力してください。\n' + 'イベントの種類:' + event.type;
|
46
|
+
alert(msg);
|
47
|
+
}
|
48
|
+
} else {
|
43
|
-
$('.error').each(function() {
|
49
|
+
$('.error').each(function () {
|
44
|
-
|
50
|
+
var p = $(this).parents('.hoge');
|
45
|
-
|
51
|
+
var limit = parseInt(p.find('.limit').text(), 10);
|
46
|
-
|
52
|
+
msg += $(this).closest('.hoge').find('.name').text() + 'は' + limit + '文字以内で入力してください。\n' + 'イベントの種類:' + event.type + '\n';
|
47
|
-
|
53
|
+
flg++;
|
48
54
|
});
|
49
55
|
if (flg !== 0) {
|
50
|
-
|
56
|
+
$(checkTgt).off('blur');
|
57
|
+
event.preventDefault();
|
51
|
-
|
58
|
+
alert(msg);
|
52
|
-
} else {
|
53
|
-
$(checkTgt).off('blur');
|
54
|
-
event.preventDefault();
|
55
|
-
alert(msg);
|
56
|
-
|
59
|
+
$(checkTgt).blur();
|
57
|
-
}
|
58
60
|
}
|
61
|
+
}
|
59
62
|
}
|
60
63
|
// 入力文字数表示
|
61
64
|
function cntChar(elm) {
|
@@ -80,7 +83,7 @@
|
|
80
83
|
$('.txb').on('blur', function() {
|
81
84
|
limitCheck();
|
82
85
|
});
|
83
|
-
$('.btn').on('click', function(
|
86
|
+
$('.btn').on('click', function() {
|
84
87
|
limitCheck();
|
85
88
|
});
|
86
89
|
});
|