-
Notifications
You must be signed in to change notification settings - Fork 123
/
demo.html
182 lines (170 loc) · 6 KB
/
demo.html
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta name="viewport" content="user-scalable=yes, width=device-width, initial-scale=1" />
<title>Happy.js | Demo</title>
<style>
*, *:before, *:after {
box-sizing: inherit;
}
html {
font-size: 16px;
}
body {
box-sizing: border-box;
font-family: Helvetica;
background: #eee;
padding: 0 1rem;
}
h1 {
font-size: 1.5rem;
color: #2196F3;
margin: 0 0 1rem;
}
form {
margin: 0;
}
input {
display: block;
}
.unhappyMessage {
color: #F44336;
display: block;
}
.card {
background: #fff;
padding: 1rem;
margin: 1rem auto;
max-width: 360px;
box-shadow: 0 1px 1px rgba(0,0,0,0.15) ,
0 1px 2px rgba(0,0,0,0.1);
}
input,
button,
.unhappyMessage {
display: block;
width: 100%;
border: 0;
padding: 10px 15px;
}
input {
background: #eee;
margin-bottom: 1rem;
transition: box-shadow .05s;
}
input:focus {
outline: 0;
box-shadow: inset 0 1px 2px rgba(0,0,0,.15);
}
.unhappyMessage {
margin-top: -1em;
margin-bottom: 1rem;
padding-left: 0;
padding-right: 0;
}
button {
appearance: none;
background: #4CAF50;
color: #fff;
text-align: center;
transition: background .2s;
}
button:hover, button:focus {
outline: 0;
background: #8BC34A;
}
</style>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="happy.js"></script>
<script src="happy.methods.js"></script>
<script>
function minLength(val, min) {
return val.length < min ? new Error('Your name must be longer than ' + min + ' character' + (min !== 0 ? 's' : '') + '.') : true;
}
function maxLength(val, max) {
return val.length > max ? new Error('Your name must be shorter than ' + max + ' character' + (max !== 0 ? 's' : '') + '.') : true;
}
$(document).ready(function () {
$('#awesomeForm').isHappy({
fields: {
// reference the field you're talking about, probably by `id`
// but you could certainly do $('[name=name]') as well.
'#yourName': {
required: true,
message: 'Might we inquire your name',
test: [minLength, maxLength],
arg: [3, 64]
},
'#email': {
required: true,
message: 'How are we to reach you sans email??',
test: happy.email // this can be *any* function that returns true or false
},
'#birthday': {
required: true,
message: 'Please provide a valid birth date.',
test: function(value) {
var birthday = new Date(value);
var birthdayInt = birthday.getTime();
if (isNaN(birthdayInt)) {
// uses the default message.
return false;
}
if(birthdayInt > new Date()) {
return new Error('Your birthday must have already happened.');
}
if (birthday.getDay() === 3) {
return new Error('Your birthday cannot have happened on a Wednesday.');
}
return true;
}
}
}
});
$('#awesomeFormSubmitButton').isHappy({
submitButton: '#submitTheForm',
fields: {
// reference the field you're talking about, probably by `id`
// but you could certainly do $('[name=name]') as well.
'#yourName-2': {
required: true,
message: 'Might we inquire your name'
},
'#email-2': {
required: true,
message: 'How are we to reach you sans email??',
test: happy.email // this can be *any* function that returns true or false
}
}
});
});
</script>
</head>
<body>
<div class="card">
<h1>Simple Form</h1>
<form id="awesomeForm" action="/lights/camera" method="post">
<label for="yourName">Name</label>
<input id="yourName" type="text" name="name" />
<label for="email">Email</label>
<input id="email" type="email" name="email" />
<label for="birthday">Birthday</label>
<input id="birthday" type="text" name="birthday" />
<button type="submit">go</button>
</form>
</div>
<div class="card">
<h1>With config.submitButton</h1>
<form id="awesomeFormSubmitButton" action="/lights/camera" method="post">
<label for="yourName-2">Name</label>
<input id="yourName-2" type="text" name="name" />
<label for="email-2">Email</label>
<input id="email-2" type="email" name="email" />
</form>
</div>
<div class="card">
<button id="submitTheForm">Submit the form</button>
</div>
</body>
</html>