blob: 3f8c7acacbb76129faa73525b40c2a69c91f01b5 [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 }
18 form.classList.add('was-validated');
19 }, false);
20 });
21 }, false);
22})();
23
Marc Kupietz145f5b92023-03-09 20:39:31 +010024function update_total_due() {
Marc Kupietz0be2b462023-03-08 09:29:11 +010025 var costs = 0;
Marc Kupietz0dbe6b62023-03-21 17:58:11 +010026 var vegetarian_dinner = $("#vegetarian_dinner");
Marc Kupietz0be2b462023-03-08 09:29:11 +010027
Marc Kupietz0dbe6b62023-03-21 17:58:11 +010028 if ($('#conference_dinner').is(":checked")) {
29 vegetarian_dinner.removeAttr('disabled');
30 } else {
31 vegetarian_dinner.attr('disabled', 'disabled');
32 }
Marc Kupietz0be2b462023-03-08 09:29:11 +010033
34 if ($('#participate').is(":checked")) {
Marc Kupietzf1903162023-03-10 13:54:23 +010035 costs += parseInt($("#conference_fee").text(), 10);
Marc Kupietz0be2b462023-03-08 09:29:11 +010036 if ($('#student').is(":checked")) {
37 costs -= parseInt($("#student_discount").text(), 10);
38 }
39 if ($('#conference_dinner').is(":checked")) {
40 costs += parseInt($("#conference_dinner_price").text(), 10);
41 }
Marc Kupietz79eaa0b2023-03-16 17:33:43 +010042 const lunches = ["lunch_day_1", "lunch_day_2", "lunch_day_3"];
43 lunches.forEach(function(lunch) {
44 if ($("input[name='" + lunch + "']:checked").val() != "--") {
45 costs += parseInt($("#lunch_costs").text().replace(/[^0-9]/g, ''), 10) / 100;
46 }
47 });
Marc Kupietz0be2b462023-03-08 09:29:11 +010048 }
Marc Kupietzf1903162023-03-10 13:54:23 +010049 $("#total_due").val(costs.toLocaleString('en-US', { minimumFractionDigits: 2, maximumFractionDigits: 2 }));
Marc Kupietz0be2b462023-03-08 09:29:11 +010050}
51
52function update_paper_id_field() {
53 var paper_id = $("#paper_id");
54 if ($('#author').is(":checked")) {
55 paper_id.removeAttr('disabled');
Marc Kupietzd2e5d272023-03-16 17:35:19 +010056 paper_id.attr('pattern', '\\d{1,4}');
Marc Kupietz0be2b462023-03-08 09:29:11 +010057 paper_id.attr('required', 'required');
Marc Kupietzce13e962023-03-21 17:54:33 +010058 val = paper_id.val();
59 if (!x.match(/^\d{1,4}$/)) {
60 paper_id.val('');
61 }
Marc Kupietz0be2b462023-03-08 09:29:11 +010062 } else {
Marc Kupietz0be2b462023-03-08 09:29:11 +010063 paper_id.attr('disabled', 'disabled');
Marc Kupietzce13e962023-03-21 17:54:33 +010064 paper_id.val('');
Marc Kupietz0be2b462023-03-08 09:29:11 +010065 paper_id.removeAttr('placeholder');
66 paper_id.removeAttr('pattern');
67 paper_id.removeAttr('required');
68 }
69}
Marc Kupietz484ec8e2023-02-25 11:23:07 +010070
71function check_password_match() {
72 pass = $("#pw1").val();
73 pass2 = $("#pw2").val();
74
75 if (pass2.length > 0) {
76 $("#pwconfirm")[0].classList.add('was-validated')
77 if (pass != pass2) {
78 $("#pw2")[0].setCustomValidity("Passwords do not match");
79 } else {
80 $("#pw2")[0].setCustomValidity(""); // is valid
81 }
82 } else {
83 $("#pwconfirm")[0].classList.remove('was-validated')
84 }
85 passUpdated();
86}
87
88TOO_SHORT = 'Password too short, still %% characters needed';
89TOO_LONG = 'Password too long, please remove %% characters';
90INVALID_CHARS = 'Password contains invalid characters';
91QUAL_NONE = 'Password is very weak'
92QUAL_LOW = 'Password is weak';
93QUAL_MEDIUM = 'Password is average'
94QUAL_GOOD = 'Password is good';
95QUAL_STRONG = 'Password is strong';
96REP_OK = 'Repetition ok';
97REP_NE = 'Passwords not identical';
98PWNED = 'Password found in public password list';
99
Marc Kupietzf1903162023-03-10 13:54:23 +0100100String.prototype.strReverse = function() {
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100101 var newstring = '';
102 for (var s = 0; s < this.length; s++)
103 newstring = this.charAt(s) + newstring;
104 return newstring;
105};
106
107function isPasswordPwned(pass, callback) {
108 const sha1Hash = CryptoJS.enc.Hex.stringify(CryptoJS.SHA1(pass)).toUpperCase();
109 const passwordChunk1 = sha1Hash.substring(0, 5);
110 const passwordChunk2 = sha1Hash.substring(5);
Marc Kupietzf1903162023-03-10 13:54:23 +0100111 $.get('https://api.pwnedpasswords.com/range/' + passwordChunk1).done(function(data, status) {
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100112 var isPwned = false;
113 if (status == "success") {
114 if (data && data.length) {
115 const chunks = data.split('\r\n');
116 const matches = chunks.filter(s => s.includes(passwordChunk2));
117
118 if (matches.length) {
119 isPwned = true
120 }
121 }
122 callback(isPwned)
123 }
124 });
125}
126
127//var checkTimer;
128
129function passUpdated() {
130 var nScore = 0;
131 var message = '';
132
133 var pass = $('#pw1').val();
134 var pass2 = $('#pw2').val();
135
136 //clearTimeout(checkTimer);
137
138 try {
139 if (!pass)
140 throw '';
141
142 if (pass.match(/[^a-zA-Z0-9!@#$%()_+=:;",.?/-]/))
143 throw INVALID_CHARS;
144
145 var nLength = pass.length;
146 if (nLength < 8)
147 throw TOO_SHORT.replace('%%', 8 - nLength);
148 if (nLength > 20)
149 throw TOO_LONG.replace('%%', nLength - 20);
150
151 nScore = 4 * nLength;
152
153 // check for upper-/lowercase, numeric and special chars pattern matches
Marc Kupietzf1903162023-03-10 13:54:23 +0100154 var nAlphaUC = 0,
155 nAlphaLC = 0,
156 nNumber = 0,
157 nSpecial = 0;
158 var nMidChar = 0,
159 nRepChar = 0,
160 nRepInc = 0;
161 var nConsecAlphaUC = 0,
162 nConsecAlphaLC = 0,
163 nConsecNumber = 0;
164 var nTmpAlphaUC = '',
165 nTmpAlphaLC = '',
166 nTmpNumber = '';
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100167 for (var i = 0; i < nLength; i++) {
Marc Kupietzf1903162023-03-10 13:54:23 +0100168 if (pass[i].match(/[A-Z]/g)) { // uppercase characters
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100169 if (nTmpAlphaUC !== '' && (nTmpAlphaUC + 1) == i) {
170 nConsecAlphaUC++;
171 }
172 nTmpAlphaUC = i;
173 nAlphaUC++;
Marc Kupietzf1903162023-03-10 13:54:23 +0100174 } else if (pass[i].match(/[a-z]/g)) { // lowercase characters
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100175 if (nTmpAlphaLC !== '' && (nTmpAlphaLC + 1) == i) {
176 nConsecAlphaLC++;
177 }
178 nTmpAlphaLC = i;
179 nAlphaLC++;
Marc Kupietzf1903162023-03-10 13:54:23 +0100180 } else if (pass[i].match(/[0-9]/g)) { // numbers
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100181 if (i > 0 && i < (nLength - 1)) {
182 nMidChar++;
183 }
184 if (nTmpNumber !== '' && (nTmpNumber + 1) == i) {
185 nConsecNumber++;
186 }
187 nTmpNumber = i;
188 nNumber++;
Marc Kupietzf1903162023-03-10 13:54:23 +0100189 } else { // special characters
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100190 if (i > 0 && i < (nLength - 1)) {
191 nMidChar++;
192 }
193 nSpecial++;
194 }
195
196 // check for repeated characters
197 var bCharExists = false;
198 for (var j = 0; j < nLength; j++) {
199 if (pass[i] == pass[j] && i != j) {
200 bCharExists = true;
201 nRepInc += Math.abs(nLength / (j - i));
202 }
203 }
204 if (bCharExists) {
205 nRepChar++;
206 var nUnqChar = nLength - nRepChar;
207 nRepInc = (nUnqChar) ? Math.ceil(nRepInc / nUnqChar) : Math.ceil(nRepInc);
208 }
209 }
210
211 // check for sequential alpha string patterns (forward and reverse)
212 var sAlphas = "abcdefghijklmnopqrstuvwxyz";
213 var nSeqAlpha = 0;
214 for (var i = 0; i < 23; i++) {
215 var sFwd = sAlphas.substring(i, i + 3);
216 var sRev = sFwd.strReverse();
Marc Kupietzf1903162023-03-10 13:54:23 +0100217 if (pass.toLowerCase().indexOf(sFwd) != -1 ||
218 pass.toLowerCase().indexOf(sRev) != -1)
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100219 nSeqAlpha++;
220 }
221
222 // check for sequential numeric string patterns (forward and reverse)
223 var sNumerics = "01234567890";
224 var nSeqNumber = 0;
225 for (var i = 0; i < 8; i++) {
226 var sFwd = sNumerics.substring(i, i + 3);
227 var sRev = sFwd.strReverse();
Marc Kupietzf1903162023-03-10 13:54:23 +0100228 if (pass.toLowerCase().indexOf(sFwd) != -1 ||
229 pass.toLowerCase().indexOf(sRev) != -1)
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100230 nSeqNumber++;
231 }
232
233 // general point assignment
Marc Kupietzf1903162023-03-10 13:54:23 +0100234 if (nAlphaUC > 0 && nAlphaUC < nLength) // uppercase characters
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100235 nScore += 2 * (nLength - nAlphaUC);
Marc Kupietzf1903162023-03-10 13:54:23 +0100236 if (nAlphaLC > 0 && nAlphaLC < nLength) // lowercase characters
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100237 nScore += 2 * (nLength - nAlphaLC);
Marc Kupietzf1903162023-03-10 13:54:23 +0100238 if (nNumber > 0 && nNumber < nLength) // numbers
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100239 nScore += 2 * nNumber;
Marc Kupietzf1903162023-03-10 13:54:23 +0100240 if (nSpecial > 0) // special characters
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100241 nScore += 4 * nSpecial;
Marc Kupietzf1903162023-03-10 13:54:23 +0100242 if (nMidChar > 0) // mid numbers/special characters
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100243 nScore += 2 * nMidChar;
244
245 // point deductions for poor practices
Marc Kupietzf1903162023-03-10 13:54:23 +0100246 if ((nAlphaLC > 0 || nAlphaUC > 0) &&
247 nSpecial === 0 && nNumber === 0) // characters only
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100248 nScore -= nLength;
Marc Kupietzf1903162023-03-10 13:54:23 +0100249 if (nAlphaLC === 0 && nAlphaUC === 0 &&
250 nSpecial === 0 && nNumber > 0) // numbers only
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100251 nScore -= nLength;
Marc Kupietzf1903162023-03-10 13:54:23 +0100252 if (nRepChar > 0) // same character exists more than once
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100253 nScore -= nRepInc;
Marc Kupietzf1903162023-03-10 13:54:23 +0100254 if (nConsecAlphaUC > 0) // consecutive uppercase letters exist
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100255 nScore -= 2 * nConsecAlphaUC;
Marc Kupietzf1903162023-03-10 13:54:23 +0100256 if (nConsecAlphaLC > 0) // consecutive lowercase letters exist
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100257 nScore -= 2 * nConsecAlphaLC;
Marc Kupietzf1903162023-03-10 13:54:23 +0100258 if (nConsecNumber > 0) // consecutive numbers exist
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100259 nScore -= 2 * nConsecNumber;
Marc Kupietzf1903162023-03-10 13:54:23 +0100260 if (nSeqAlpha > 0) // sequential alpha strings exist (3 chars or more)
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100261 nScore -= 3 * nSeqAlpha;
Marc Kupietzf1903162023-03-10 13:54:23 +0100262 if (nSeqNumber > 0) // sequential numeric strings exist (3 chars or more)
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100263 nScore -= 3 * nSeqNumber;
264
265 // determine if mandatory requirements have been met
266 var arrChars = [nAlphaUC, nAlphaLC, nNumber, nSpecial];
267 var nReqChar = 0;
268 for (var i = 0; i < arrChars.length; i++) {
269 if (arrChars[i]) {
270 nReqChar++;
271 }
272 }
273 if (nReqChar >= arrChars.length)
274 nScore += 2 * nReqChar;
275 else if (nReqChar < arrChars.length - 1)
276 nScore -= 2 * nReqChar;
277
278 // limit points to 3..100
279 nScore = Math.max(3, Math.min(nScore, 100));
280
281 // set message according to points
282 if (nScore >= 80)
283 message = QUAL_STRONG;
284 else if (nScore >= 60)
285 message = QUAL_GOOD;
286 else if (nScore >= 40)
287 message = QUAL_MEDIUM;
288 else if (nScore >= 10)
289 message = QUAL_LOW;
290 else
291 message = QUAL_NONE;
292 } catch (error) {
293 nScore = 3;
294 message = error;
295 }
296 /*
297 if (pass.length > 0 && pass2.length > 0) {
298 message+=' / ';
299 message+=(pass == pass2) ? REP_OK : REP_NE;
300 }
301 */
302 $('#pwqinfo').val(message);
303
304 var progress = $('#pwqbar');
305 progress.width(nScore + '%');
306 progress.attr('aria-valuenow', nScore);
307 if (nScore >= 60)
308 progress.removeClass('bg-danger bg-warning').addClass('bg-success');
309 else if (nScore >= 40)
310 progress.removeClass('bg-danger bg-success').addClass('bg-warning');
311 else
312 progress.removeClass('bg-warning bg-success').addClass('bg-danger');
313
314 if (nScore >= 40) {
315 $("#password-div")[0].classList.add('was-validated');
316 $('#btn_change').prop('disabled', pass !== pass2);
317 $("#pw1")[0].setCustomValidity("");
Marc Kupietzf1903162023-03-10 13:54:23 +0100318 isPasswordPwned(pass, function(isPwned) {
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100319 if (isPwned) {
320 $('#pwqinfo').val(PWNED);
321 progress.removeClass().addClass('low');
322 progress.val(3);
323 $("#pw1")[0].setCustomValidity("Password is pwned");
324 }
325 });
326 } else {
327 $("#password-div")[0].classList.add('was-validated');
328 $('#btn_change').prop('disabled', 1);
329 $("#pw1")[0].setCustomValidity("Password is to week");
330 }
331
Marc Kupietzf1903162023-03-10 13:54:23 +0100332}