option-target.html
2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Target</title>
<link rel="stylesheet" href="demo.css">
<script src="https://cdn.jsdelivr.net/jquery/1.12.3/jquery.min.js"></script>
<script src="../dist/jquery.validator.js?local=en"></script>
</head>
<body>
<form class="form" data-validator-option="{timely:2, theme:'yellow_right'}">
<h3>Target to other input</h3>
<div class="form-item">
<label class="label">Name</label>
<input type="text" class="input-sm" name="firstName" data-target="#lastName" data-rule="required" placeholder="First Name">
<input type="text" class="input-sm" name="lastName" id="lastName" data-rule="required" placeholder="Last Name">
</div>
<div class="form-item">
<button type="submit">Submit</button>
</div>
</form>
<form class="form" data-validator-option="{timely:2, theme:'simple_right'}">
<h3>Target to "msg-box"</h3>
<div class="form-item">
<input type="text" name="name" data-rule="Name:required" data-target="#holdMsg" placeholder="Name">
</div>
<div class="form-item">
<button type="submit">Submit</button>
<span class="msg-box" id="holdMsg"></span>
</div>
</form>
<form class="form" data-validator-option="{timely:2}">
<h3>Target to a container</h3>
<div id="msgContainer"></div>
<div class="form-item">
<input type="text" name="name" data-rule="Name:required" data-target="#msgContainer">
</div>
<div class="form-item">
<button type="submit">Submit</button>
</div>
</form>
<form class="form" id="form4">
<h3>Dynamic message place.</h3>
<div class="form-item">
<label class="label">Name</label>
<input type="text" class="input-sm" name="firstName" data-rule="required" placeholder="First Name">
<input type="text" class="input-sm" name="lastName" data-rule="required" placeholder="Last Name">
</div>
<div class="form-item">
<button type="submit">Submit</button>
</div>
</form>
<script>
$('#form4').validator({
timely:2,
stopOnError: true,
display: function(input) {
return $(input).attr('placeholder');
},
target: function(input) {
var $formitem = $(input).closest('.form-item'),
$msgbox = $formitem.find('span.msg-box');
if (!$msgbox.length) {
$msgbox = $('<span class="msg-box"></span>').appendTo($formitem);
}
return $msgbox;
}
});
</script>
</body>
</html>