-
Notifications
You must be signed in to change notification settings - Fork 1
/
test-page-lite.html
executable file
·275 lines (256 loc) · 14.9 KB
/
test-page-lite.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Test Page Lite | formJS - JavaScript Form Validation. Made Easy</title>
<meta name="description" content="">
<meta name="keywords" content="">
<meta name="author" content="Valerio Di Punzio">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link href="https://fonts.googleapis.com/css?family=Expletus+Sans:500|Raleway:200&display=swap" rel="stylesheet">
<link rel="stylesheet" href="css/bootstrap.min.css">
<link rel="stylesheet" href="css/doc.css">
<link rel="stylesheet" href="css/demo.css">
</head>
<body>
<div class="container mb-5">
<div id="top" class="text-center mb-5">
<div>
<p class="mt-5 mb-4">
<a href="index.html">← back to documentation</a>
</p>
</div>
<h1>formJS</h1>
<p>Test Page Lite</p>
</div>
<!-- Demos -->
<div id="demos" class="panel-collapsible row justify-content-center">
<!-- Demo Ajax Form ( POST ) JS Error Messages -->
<div id="demo-00" class="col-12 col-md-7 col-lg-6 col-xl-5 py-3">
<div class="panel no-bg">
<div class="card-header bg-white text-dark">
<div>
<p class="m-0">Form</p>
</div>
</div>
<div class="card-body">
<form method="POST" action="json/data.json" name="ajax-form-post-js-errors">
<div data-formjs-question class="form-group">
<div class="answers-box label-move-up">
<input name="name" type="text" data-length="[3,20]" class="form-control" required />
<label>Name</label>
</div>
<div class="field-error-message small"></div>
</div>
<div data-formjs-question class="form-group">
<div class="answers-box label-move-up">
<input name="email" type="email" class="form-control" required />
<label>Email</label>
</div>
<div class="field-error-message small"></div>
</div>
<div data-formjs-question class="form-group">
<div class="answers-box label-move-up">
<input name="username" type="text" data-subtype="username" data-async-validation-url="./remoteValidations/username.php" class="form-control" required />
<label>Username</label>
</div>
<div class="field-error-message small"></div>
</div>
<div data-formjs-question class="form-group">
<div class="answers-box label-move-up">
<input name="password" type="password" class="form-control" required />
<label>Password</label>
</div>
<div class="field-error-message small"></div>
</div>
<div data-formjs-question class="form-group">
<div class="answers-box label-move-up">
<input name="dateOfBirth" type="date" min="1980-01-11" max="2050-12-30" class="form-control" required />
<label>Date of Birth (type date)</label>
</div>
<div class="field-error-message small"></div>
</div>
<div data-formjs-question class="form-group">
<div class="answers-box label-move-up">
<input name="dateOfBirth-YYYY-MM-DD" type="text" data-subtype="date" min="1980-01-11" max="2050-12-30" data-date-format="YYYY-MM-DD" class="form-control" required />
<label>Date of Birth (subtype date YYYY-MM-DD)</label>
</div>
<div class="field-error-message small"></div>
</div>
<div data-formjs-question class="form-group">
<div class="answers-box label-move-up">
<input name="dateOfBirth-YYYY/MM/DD" type="text" data-subtype="date" min="1980-01-11" max="2050-12-30" data-date-format="YYYY MM DD" class="form-control" required />
<label>Date of Birth (subtype date YYYY MM DD)</label>
</div>
<div class="field-error-message small"></div>
</div>
<div data-formjs-question class="form-group">
<p class="mb-2 small">Select your contact preferences <br />(2 answers max)</p>
<div class="answers-box">
<div class="form-check">
<input name="contactPrefCheck" type="checkbox" value="sms" class="form-check-input" id="checkbox-1-a" required data-checks="[1,2]" />
<label class="form-check-label" for="checkbox-1-a">SMS</label>
</div>
<div class="form-check">
<input name="contactPrefCheck" type="checkbox" value="email" class="form-check-input" id="checkbox-2-a" required />
<label class="form-check-label" for="checkbox-2-a">Email</label>
</div>
<div class="form-check">
<input name="contactPrefCheck" type="checkbox" value="app-notiifcation" class="form-check-input" id="checkbox-3-a" required />
<label class="form-check-label" for="checkbox-3-a">App Notification</label>
</div>
</div>
<div class="field-error-message small">
You must select at least 1 and at most 2 answers!
</div>
</div>
<div data-formjs-question="" class="form-group">
<p class="mb-2 small">You must select one of these:</p>
<div class="input-container form-check">
<input id="radio-5-20-6-01" name="radio-list-01" value="1" type="radio" class="form-check-input" required />
<label for="radio-5-20-6-01" class="form-check-label">
Answer One
</label>
</div>
<div class="input-container form-check">
<input id="radio-5-20-6-02" name="radio-list-01" value="2" type="radio" class="form-check-input" required />
<label for="radio-5-20-6-02" class="form-check-label">
Answer Two
</label>
</div>
<div class="input-group">
<span class="input-group-prepend">
<div class="input-group-text">
<input id="radio-5-200-6-03" name="radio-list-01" value="3" data-require-more="" type="radio" required />
<label for="radio-5-200-6-03"> Other:</label>
</div>
</span>
<input class="form-control" name="radio-list-01-more" data-required-from="#radio-5-200-6-03" type="text" />
</div>
<div class="field-error-message small">You must choose an answer ( and fill the text field if you selected "Other" )!</div>
</div>
<div data-formjs-question class="form-group">
<div class="answers-box">
<div class="small">
I agree with the <a href="#" target="_blank">Privacy Policy</a>.
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="privacyCheck" id="privacyCheck-1-a" value="accepted" required />
<label class="form-check-label" for="privacyCheck-1-a">Yes</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="privacyCheck" id="privacyCheck-2-a" value="" required />
<label class="form-check-label" for="privacyCheck-2-a">No</label>
</div>
</div>
<div class="field-error-message small">You must agree!</div>
</div>
<div class="text-center">
<button type="submit" class="btn btn-light">SEND</button>
</div>
<div class="d-none alert mt-5 mb-4" role="alert" data-formjs-global-feedback></div>
</form>
</div>
</div>
</div>
</div>
</div>
<!-- DYNAMICALLY LOAD POLYFILLS -> REQUIRED TO SUPPORT IE 10/11 AND SOME EDGE VERSIONS -->
<script src="https://cdn.polyfill.io/v3/polyfill.min.js?features=Array.from,Element.prototype.closest,Element.prototype.matches,Promise"></script>
<script src="js/include.js"></script>
<script src="dist/lite/formjs.js"></script>
<script src="dist/additionals/validationRules.js"></script>
<script src="dist/additionals/validationErrors.js"></script>
<script>
function populatePasswords(){
var pswFields = document.querySelectorAll('form [type="password"]');
Array.from(pswFields).forEach(function(field){
field.value = 'asdAsd123';
});
}
populatePasswords();
function checkDirtyField(fieldEl){
var containerEl = fieldEl.closest('[data-formjs-question]');
containerEl.classList.remove('is-dirty');
if( fieldEl.value ){
containerEl.classList.add('is-dirty');
}
}
function setFieldValid(){
var containerEl = this.closest('[data-formjs-question]');
containerEl.classList.remove('has-error');
containerEl.classList.add('is-valid');
}
function showErrorMessage(errors){
console.log(this, errors);
var containerEl = this.closest('[data-formjs-question]');
containerEl.classList.remove('is-valid');
containerEl.classList.add('has-error');
}
function validateField(event){
var fieldEl = event.target;
if( !fieldEl.matches('input:not([type="reset"]):not([type="submit"]):not([type="button"]):not([type="hidden"]), select, textarea') ){
return;
}
var containerEl = fieldEl.closest('[data-formjs-question]');
formInstance.validateField(fieldEl)
.then(setFieldValid.bind(fieldEl))
.catch(showErrorMessage.bind(fieldEl));
}
document.querySelector('.card-header').click();
var formEl = document.querySelector('form');
var options = {
fieldOptions: {
beforeValidation: [
function beforeValidation_testPage1( fieldObj ){
console.log('--- beforeValidation_testPageLite 1 fieldObj', fieldObj);
checkDirtyField(fieldObj.fieldEl);
},
/* function beforeValidation_testPage2( fieldObj ){
console.log('--- beforeValidation_testPageLite 2 fieldObj', fieldObj);
return new Promise(function(resolve){
setTimeout(function(){
resolve( fieldObj );
}, 1000);
});
}, */
/* function beforeValidation_testPage3( fieldObj ){
console.log('--- beforeValidation_testPageLite 3 fieldObj', fieldObj);
fieldObj.aaa = 'ciao';
return fieldObj;
} */
]
}
};
var formInstance = new Form( formEl, options );
/* VALIDATE FIELDS WITH EVENT DELAGATION */
formEl.addEventListener('blur', validateField, true);
/* VALIDATE FIELDS DIRECTLY */
Array.from(document.querySelectorAll('[type="checkbox"], [type="radio"]')).forEach(function(fieldEl){
fieldEl.addEventListener('change', validateField, false);
});
formEl.addEventListener('submit', function(event){
event.preventDefault();
formInstance.validateForm()
.then(function(obj){
console.log('Form is valid');
fields.forEach(function(obj){
checkDirtyField(obj.fieldEl);
setFieldValid.call(obj.fieldEl);
});
})
.catch(function(fields){
console.log('Form is NOT valid');
fields.forEach(function(obj){
checkDirtyField(obj.fieldEl);
if( !obj.result ){
showErrorMessage.call(obj.fieldEl, obj.errors);
}
});
});
});
</script>
<script src="dist/additionals/utils.js"></script>
</body>
</html>