blob: ee19a8ae463afc1a50cc8f40e7c52fb3e1e7ed2e [file] [log] [blame]
Marc Kupietz484ec8e2023-02-25 11:23:07 +01001// Example starter JavaScript for disabling form submissions if there are invalid fields
Marc Kupietzf1903162023-03-10 13:54:23 +01002(function() {
Marc Kupietz484ec8e2023-02-25 11:23:07 +01003 'use strict';
Marc Kupietzf1903162023-03-10 13:54:23 +01004 window.addEventListener('load', function() {
Marc Kupietz4fc9eeb2023-03-21 17:56:34 +01005 const tooltipTriggerList = document.querySelectorAll('[data-bs-toggle="tooltip"]')
6 const tooltipList = [...tooltipTriggerList].map(tooltipTriggerEl => new bootstrap.Tooltip(tooltipTriggerEl))
Marc Kupietz145f5b92023-03-09 20:39:31 +01007 update_total_due();
Marc Kupietz8252b6d2023-03-10 06:46:32 +01008 update_paper_id_field();
Marc Kupietz484ec8e2023-02-25 11:23:07 +01009 // Fetch all the forms we want to apply custom Bootstrap validation styles to
10 var forms = document.getElementsByClassName('needs-validation');
11 // Loop over them and prevent submission
Marc Kupietzf1903162023-03-10 13:54:23 +010012 var validation = Array.prototype.filter.call(forms, function(form) {
13 form.addEventListener('submit', function(event) {
Marc Kupietz484ec8e2023-02-25 11:23:07 +010014 if (form.checkValidity() === false) {
15 event.preventDefault();
16 event.stopPropagation();
17 }
Marc Kupietz8b80c7a2023-03-28 21:41:39 +020018 update_total_due();
Marc Kupietz484ec8e2023-02-25 11:23:07 +010019 form.classList.add('was-validated');
20 }, false);
21 });
22 }, false);
Marc Kupietz8b80c7a2023-03-28 21:41:39 +020023 window.addEventListener('popstate', function(event) {
24 update_total_due();
25 }, false);
Marc Kupietz484ec8e2023-02-25 11:23:07 +010026})();
27
Marc Kupietz145f5b92023-03-09 20:39:31 +010028function update_total_due() {
Marc Kupietz0be2b462023-03-08 09:29:11 +010029 var costs = 0;
Marc Kupietz0dbe6b62023-03-21 17:58:11 +010030 var vegetarian_dinner = $("#vegetarian_dinner");
Marc Kupietz0be2b462023-03-08 09:29:11 +010031
Marc Kupietz0dbe6b62023-03-21 17:58:11 +010032 if ($('#conference_dinner').is(":checked")) {
33 vegetarian_dinner.removeAttr('disabled');
34 } else {
35 vegetarian_dinner.attr('disabled', 'disabled');
36 }
Marc Kupietz0be2b462023-03-08 09:29:11 +010037
38 if ($('#participate').is(":checked")) {
Marc Kupietzf1903162023-03-10 13:54:23 +010039 costs += parseInt($("#conference_fee").text(), 10);
Marc Kupietz0be2b462023-03-08 09:29:11 +010040 if ($('#student').is(":checked")) {
41 costs -= parseInt($("#student_discount").text(), 10);
42 }
43 if ($('#conference_dinner').is(":checked")) {
44 costs += parseInt($("#conference_dinner_price").text(), 10);
45 }
Marc Kupietz8b80c7a2023-03-28 21:41:39 +020046 if ($('#excursion').is(":checked")) {
47 costs += parseInt($("#excursion_price").text(), 10);
48 }
Marc Kupietz79eaa0b2023-03-16 17:33:43 +010049 const lunches = ["lunch_day_1", "lunch_day_2", "lunch_day_3"];
50 lunches.forEach(function(lunch) {
51 if ($("input[name='" + lunch + "']:checked").val() != "--") {
52 costs += parseInt($("#lunch_costs").text().replace(/[^0-9]/g, ''), 10) / 100;
53 }
54 });
Marc Kupietz0be2b462023-03-08 09:29:11 +010055 }
Marc Kupietzf1903162023-03-10 13:54:23 +010056 $("#total_due").val(costs.toLocaleString('en-US', { minimumFractionDigits: 2, maximumFractionDigits: 2 }));
Marc Kupietz0be2b462023-03-08 09:29:11 +010057}
58
59function update_paper_id_field() {
60 var paper_id = $("#paper_id");
61 if ($('#author').is(":checked")) {
62 paper_id.removeAttr('disabled');
Marc Kupietzd2e5d272023-03-16 17:35:19 +010063 paper_id.attr('pattern', '\\d{1,4}');
Marc Kupietz0be2b462023-03-08 09:29:11 +010064 paper_id.attr('required', 'required');
Marc Kupietzce13e962023-03-21 17:54:33 +010065 val = paper_id.val();
66 if (!x.match(/^\d{1,4}$/)) {
67 paper_id.val('');
68 }
Marc Kupietz0be2b462023-03-08 09:29:11 +010069 } else {
Marc Kupietz0be2b462023-03-08 09:29:11 +010070 paper_id.attr('disabled', 'disabled');
Marc Kupietzce13e962023-03-21 17:54:33 +010071 paper_id.val('');
Marc Kupietz0be2b462023-03-08 09:29:11 +010072 paper_id.removeAttr('placeholder');
73 paper_id.removeAttr('pattern');
74 paper_id.removeAttr('required');
75 }
76}
Marc Kupietz484ec8e2023-02-25 11:23:07 +010077
78function check_password_match() {
79 pass = $("#pw1").val();
80 pass2 = $("#pw2").val();
81
82 if (pass2.length > 0) {
83 $("#pwconfirm")[0].classList.add('was-validated')
84 if (pass != pass2) {
85 $("#pw2")[0].setCustomValidity("Passwords do not match");
86 } else {
87 $("#pw2")[0].setCustomValidity(""); // is valid
88 }
89 } else {
90 $("#pwconfirm")[0].classList.remove('was-validated')
91 }
92 passUpdated();
93}
94
95TOO_SHORT = 'Password too short, still %% characters needed';
96TOO_LONG = 'Password too long, please remove %% characters';
97INVALID_CHARS = 'Password contains invalid characters';
98QUAL_NONE = 'Password is very weak'
99QUAL_LOW = 'Password is weak';
100QUAL_MEDIUM = 'Password is average'
101QUAL_GOOD = 'Password is good';
102QUAL_STRONG = 'Password is strong';
103REP_OK = 'Repetition ok';
104REP_NE = 'Passwords not identical';
105PWNED = 'Password found in public password list';
106
Marc Kupietzf1903162023-03-10 13:54:23 +0100107String.prototype.strReverse = function() {
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100108 var newstring = '';
109 for (var s = 0; s < this.length; s++)
110 newstring = this.charAt(s) + newstring;
111 return newstring;
112};
113
114function isPasswordPwned(pass, callback) {
115 const sha1Hash = CryptoJS.enc.Hex.stringify(CryptoJS.SHA1(pass)).toUpperCase();
116 const passwordChunk1 = sha1Hash.substring(0, 5);
117 const passwordChunk2 = sha1Hash.substring(5);
Marc Kupietzf1903162023-03-10 13:54:23 +0100118 $.get('https://api.pwnedpasswords.com/range/' + passwordChunk1).done(function(data, status) {
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100119 var isPwned = false;
120 if (status == "success") {
121 if (data && data.length) {
122 const chunks = data.split('\r\n');
123 const matches = chunks.filter(s => s.includes(passwordChunk2));
124
125 if (matches.length) {
126 isPwned = true
127 }
128 }
129 callback(isPwned)
130 }
131 });
132}
133
134//var checkTimer;
135
136function passUpdated() {
137 var nScore = 0;
138 var message = '';
139
140 var pass = $('#pw1').val();
141 var pass2 = $('#pw2').val();
142
143 //clearTimeout(checkTimer);
144
145 try {
146 if (!pass)
147 throw '';
148
149 if (pass.match(/[^a-zA-Z0-9!@#$%()_+=:;",.?/-]/))
150 throw INVALID_CHARS;
151
152 var nLength = pass.length;
153 if (nLength < 8)
154 throw TOO_SHORT.replace('%%', 8 - nLength);
155 if (nLength > 20)
156 throw TOO_LONG.replace('%%', nLength - 20);
157
158 nScore = 4 * nLength;
159
160 // check for upper-/lowercase, numeric and special chars pattern matches
Marc Kupietzf1903162023-03-10 13:54:23 +0100161 var nAlphaUC = 0,
162 nAlphaLC = 0,
163 nNumber = 0,
164 nSpecial = 0;
165 var nMidChar = 0,
166 nRepChar = 0,
167 nRepInc = 0;
168 var nConsecAlphaUC = 0,
169 nConsecAlphaLC = 0,
170 nConsecNumber = 0;
171 var nTmpAlphaUC = '',
172 nTmpAlphaLC = '',
173 nTmpNumber = '';
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100174 for (var i = 0; i < nLength; i++) {
Marc Kupietzf1903162023-03-10 13:54:23 +0100175 if (pass[i].match(/[A-Z]/g)) { // uppercase characters
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100176 if (nTmpAlphaUC !== '' && (nTmpAlphaUC + 1) == i) {
177 nConsecAlphaUC++;
178 }
179 nTmpAlphaUC = i;
180 nAlphaUC++;
Marc Kupietzf1903162023-03-10 13:54:23 +0100181 } else if (pass[i].match(/[a-z]/g)) { // lowercase characters
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100182 if (nTmpAlphaLC !== '' && (nTmpAlphaLC + 1) == i) {
183 nConsecAlphaLC++;
184 }
185 nTmpAlphaLC = i;
186 nAlphaLC++;
Marc Kupietzf1903162023-03-10 13:54:23 +0100187 } else if (pass[i].match(/[0-9]/g)) { // numbers
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100188 if (i > 0 && i < (nLength - 1)) {
189 nMidChar++;
190 }
191 if (nTmpNumber !== '' && (nTmpNumber + 1) == i) {
192 nConsecNumber++;
193 }
194 nTmpNumber = i;
195 nNumber++;
Marc Kupietzf1903162023-03-10 13:54:23 +0100196 } else { // special characters
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100197 if (i > 0 && i < (nLength - 1)) {
198 nMidChar++;
199 }
200 nSpecial++;
201 }
202
203 // check for repeated characters
204 var bCharExists = false;
205 for (var j = 0; j < nLength; j++) {
206 if (pass[i] == pass[j] && i != j) {
207 bCharExists = true;
208 nRepInc += Math.abs(nLength / (j - i));
209 }
210 }
211 if (bCharExists) {
212 nRepChar++;
213 var nUnqChar = nLength - nRepChar;
214 nRepInc = (nUnqChar) ? Math.ceil(nRepInc / nUnqChar) : Math.ceil(nRepInc);
215 }
216 }
217
218 // check for sequential alpha string patterns (forward and reverse)
219 var sAlphas = "abcdefghijklmnopqrstuvwxyz";
220 var nSeqAlpha = 0;
221 for (var i = 0; i < 23; i++) {
222 var sFwd = sAlphas.substring(i, i + 3);
223 var sRev = sFwd.strReverse();
Marc Kupietzf1903162023-03-10 13:54:23 +0100224 if (pass.toLowerCase().indexOf(sFwd) != -1 ||
225 pass.toLowerCase().indexOf(sRev) != -1)
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100226 nSeqAlpha++;
227 }
228
229 // check for sequential numeric string patterns (forward and reverse)
230 var sNumerics = "01234567890";
231 var nSeqNumber = 0;
232 for (var i = 0; i < 8; i++) {
233 var sFwd = sNumerics.substring(i, i + 3);
234 var sRev = sFwd.strReverse();
Marc Kupietzf1903162023-03-10 13:54:23 +0100235 if (pass.toLowerCase().indexOf(sFwd) != -1 ||
236 pass.toLowerCase().indexOf(sRev) != -1)
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100237 nSeqNumber++;
238 }
239
240 // general point assignment
Marc Kupietzf1903162023-03-10 13:54:23 +0100241 if (nAlphaUC > 0 && nAlphaUC < nLength) // uppercase characters
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100242 nScore += 2 * (nLength - nAlphaUC);
Marc Kupietzf1903162023-03-10 13:54:23 +0100243 if (nAlphaLC > 0 && nAlphaLC < nLength) // lowercase characters
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100244 nScore += 2 * (nLength - nAlphaLC);
Marc Kupietzf1903162023-03-10 13:54:23 +0100245 if (nNumber > 0 && nNumber < nLength) // numbers
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100246 nScore += 2 * nNumber;
Marc Kupietzf1903162023-03-10 13:54:23 +0100247 if (nSpecial > 0) // special characters
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100248 nScore += 4 * nSpecial;
Marc Kupietzf1903162023-03-10 13:54:23 +0100249 if (nMidChar > 0) // mid numbers/special characters
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100250 nScore += 2 * nMidChar;
251
252 // point deductions for poor practices
Marc Kupietzf1903162023-03-10 13:54:23 +0100253 if ((nAlphaLC > 0 || nAlphaUC > 0) &&
254 nSpecial === 0 && nNumber === 0) // characters only
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100255 nScore -= nLength;
Marc Kupietzf1903162023-03-10 13:54:23 +0100256 if (nAlphaLC === 0 && nAlphaUC === 0 &&
257 nSpecial === 0 && nNumber > 0) // numbers only
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100258 nScore -= nLength;
Marc Kupietzf1903162023-03-10 13:54:23 +0100259 if (nRepChar > 0) // same character exists more than once
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100260 nScore -= nRepInc;
Marc Kupietzf1903162023-03-10 13:54:23 +0100261 if (nConsecAlphaUC > 0) // consecutive uppercase letters exist
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100262 nScore -= 2 * nConsecAlphaUC;
Marc Kupietzf1903162023-03-10 13:54:23 +0100263 if (nConsecAlphaLC > 0) // consecutive lowercase letters exist
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100264 nScore -= 2 * nConsecAlphaLC;
Marc Kupietzf1903162023-03-10 13:54:23 +0100265 if (nConsecNumber > 0) // consecutive numbers exist
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100266 nScore -= 2 * nConsecNumber;
Marc Kupietzf1903162023-03-10 13:54:23 +0100267 if (nSeqAlpha > 0) // sequential alpha strings exist (3 chars or more)
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100268 nScore -= 3 * nSeqAlpha;
Marc Kupietzf1903162023-03-10 13:54:23 +0100269 if (nSeqNumber > 0) // sequential numeric strings exist (3 chars or more)
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100270 nScore -= 3 * nSeqNumber;
271
272 // determine if mandatory requirements have been met
273 var arrChars = [nAlphaUC, nAlphaLC, nNumber, nSpecial];
274 var nReqChar = 0;
275 for (var i = 0; i < arrChars.length; i++) {
276 if (arrChars[i]) {
277 nReqChar++;
278 }
279 }
280 if (nReqChar >= arrChars.length)
281 nScore += 2 * nReqChar;
282 else if (nReqChar < arrChars.length - 1)
283 nScore -= 2 * nReqChar;
284
285 // limit points to 3..100
286 nScore = Math.max(3, Math.min(nScore, 100));
287
288 // set message according to points
289 if (nScore >= 80)
290 message = QUAL_STRONG;
291 else if (nScore >= 60)
292 message = QUAL_GOOD;
293 else if (nScore >= 40)
294 message = QUAL_MEDIUM;
295 else if (nScore >= 10)
296 message = QUAL_LOW;
297 else
298 message = QUAL_NONE;
299 } catch (error) {
300 nScore = 3;
301 message = error;
302 }
303 /*
304 if (pass.length > 0 && pass2.length > 0) {
305 message+=' / ';
306 message+=(pass == pass2) ? REP_OK : REP_NE;
307 }
308 */
309 $('#pwqinfo').val(message);
310
311 var progress = $('#pwqbar');
312 progress.width(nScore + '%');
313 progress.attr('aria-valuenow', nScore);
314 if (nScore >= 60)
315 progress.removeClass('bg-danger bg-warning').addClass('bg-success');
316 else if (nScore >= 40)
317 progress.removeClass('bg-danger bg-success').addClass('bg-warning');
318 else
319 progress.removeClass('bg-warning bg-success').addClass('bg-danger');
320
321 if (nScore >= 40) {
322 $("#password-div")[0].classList.add('was-validated');
323 $('#btn_change').prop('disabled', pass !== pass2);
324 $("#pw1")[0].setCustomValidity("");
Marc Kupietzf1903162023-03-10 13:54:23 +0100325 isPasswordPwned(pass, function(isPwned) {
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100326 if (isPwned) {
327 $('#pwqinfo').val(PWNED);
328 progress.removeClass().addClass('low');
329 progress.val(3);
330 $("#pw1")[0].setCustomValidity("Password is pwned");
331 }
332 });
333 } else {
334 $("#password-div")[0].classList.add('was-validated');
335 $('#btn_change').prop('disabled', 1);
336 $("#pw1")[0].setCustomValidity("Password is to week");
337 }
338
Marc Kupietzf1903162023-03-10 13:54:23 +0100339}