blob: e2b029c3103cd071b5aa1ad84e25028c106ab559 [file] [log] [blame]
Marc Kupietz484ec8e2023-02-25 11:23:07 +01001// Example starter JavaScript for disabling form submissions if there are invalid fields
Marc Kupietz216ff532023-03-10 13:54:23 +01002(function() {
Marc Kupietz484ec8e2023-02-25 11:23:07 +01003 'use strict';
Marc Kupietz216ff532023-03-10 13:54:23 +01004 window.addEventListener('load', function() {
Marc Kupietz87a14312023-03-09 20:39:31 +01005 update_total_due();
Marc Kupietz11de6cb2023-03-10 06:46:32 +01006 update_paper_id_field();
Marc Kupietz484ec8e2023-02-25 11:23:07 +01007 // Fetch all the forms we want to apply custom Bootstrap validation styles to
8 var forms = document.getElementsByClassName('needs-validation');
9 // Loop over them and prevent submission
Marc Kupietz216ff532023-03-10 13:54:23 +010010 var validation = Array.prototype.filter.call(forms, function(form) {
11 form.addEventListener('submit', function(event) {
Marc Kupietz484ec8e2023-02-25 11:23:07 +010012 if (form.checkValidity() === false) {
13 event.preventDefault();
14 event.stopPropagation();
15 }
16 form.classList.add('was-validated');
17 }, false);
18 });
19 }, false);
20})();
21
Marc Kupietz87a14312023-03-09 20:39:31 +010022function update_total_due() {
Marc Kupietz79c2b922023-03-08 09:29:11 +010023 var costs = 0;
24
25
26 if ($('#participate').is(":checked")) {
Marc Kupietz216ff532023-03-10 13:54:23 +010027 costs += parseInt($("#conference_fee").text(), 10);
Marc Kupietz79c2b922023-03-08 09:29:11 +010028 if ($('#student').is(":checked")) {
29 costs -= parseInt($("#student_discount").text(), 10);
30 }
31 if ($('#conference_dinner').is(":checked")) {
32 costs += parseInt($("#conference_dinner_price").text(), 10);
33 }
Marc Kupietz8701abd2023-03-16 17:33:43 +010034 const lunches = ["lunch_day_1", "lunch_day_2", "lunch_day_3"];
35 lunches.forEach(function(lunch) {
36 if ($("input[name='" + lunch + "']:checked").val() != "--") {
37 costs += parseInt($("#lunch_costs").text().replace(/[^0-9]/g, ''), 10) / 100;
38 }
39 });
Marc Kupietz79c2b922023-03-08 09:29:11 +010040 }
Marc Kupietz216ff532023-03-10 13:54:23 +010041 $("#total_due").val(costs.toLocaleString('en-US', { minimumFractionDigits: 2, maximumFractionDigits: 2 }));
Marc Kupietz79c2b922023-03-08 09:29:11 +010042}
43
44function update_paper_id_field() {
45 var paper_id = $("#paper_id");
46 if ($('#author').is(":checked")) {
47 paper_id.removeAttr('disabled');
Marc Kupietz18f2f5a2023-03-16 17:35:19 +010048 paper_id.attr('placeholder', '0');
49 paper_id.attr('pattern', '\\d{1,4}');
Marc Kupietz79c2b922023-03-08 09:29:11 +010050 paper_id.attr('required', 'required');
Marc Kupietz216ff532023-03-10 13:54:23 +010051 //if (paper_id.attr('oldval').match(/^\\d{4}$/)) {
52 // paper_id.val(paper_id.attr('oldval'));
53 //}
Marc Kupietz79c2b922023-03-08 09:29:11 +010054 } else {
Marc Kupietz216ff532023-03-10 13:54:23 +010055 paper_id.attr('oldval', paper_id.val());
Marc Kupietz79c2b922023-03-08 09:29:11 +010056 paper_id.val('');
57 paper_id.attr('disabled', 'disabled');
58 paper_id.removeAttr('placeholder');
59 paper_id.removeAttr('pattern');
60 paper_id.removeAttr('required');
61 }
62}
Marc Kupietz484ec8e2023-02-25 11:23:07 +010063
64function check_password_match() {
65 pass = $("#pw1").val();
66 pass2 = $("#pw2").val();
67
68 if (pass2.length > 0) {
69 $("#pwconfirm")[0].classList.add('was-validated')
70 if (pass != pass2) {
71 $("#pw2")[0].setCustomValidity("Passwords do not match");
72 } else {
73 $("#pw2")[0].setCustomValidity(""); // is valid
74 }
75 } else {
76 $("#pwconfirm")[0].classList.remove('was-validated')
77 }
78 passUpdated();
79}
80
81TOO_SHORT = 'Password too short, still %% characters needed';
82TOO_LONG = 'Password too long, please remove %% characters';
83INVALID_CHARS = 'Password contains invalid characters';
84QUAL_NONE = 'Password is very weak'
85QUAL_LOW = 'Password is weak';
86QUAL_MEDIUM = 'Password is average'
87QUAL_GOOD = 'Password is good';
88QUAL_STRONG = 'Password is strong';
89REP_OK = 'Repetition ok';
90REP_NE = 'Passwords not identical';
91PWNED = 'Password found in public password list';
92
Marc Kupietz216ff532023-03-10 13:54:23 +010093String.prototype.strReverse = function() {
Marc Kupietz484ec8e2023-02-25 11:23:07 +010094 var newstring = '';
95 for (var s = 0; s < this.length; s++)
96 newstring = this.charAt(s) + newstring;
97 return newstring;
98};
99
100function isPasswordPwned(pass, callback) {
101 const sha1Hash = CryptoJS.enc.Hex.stringify(CryptoJS.SHA1(pass)).toUpperCase();
102 const passwordChunk1 = sha1Hash.substring(0, 5);
103 const passwordChunk2 = sha1Hash.substring(5);
Marc Kupietz216ff532023-03-10 13:54:23 +0100104 $.get('https://api.pwnedpasswords.com/range/' + passwordChunk1).done(function(data, status) {
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100105 var isPwned = false;
106 if (status == "success") {
107 if (data && data.length) {
108 const chunks = data.split('\r\n');
109 const matches = chunks.filter(s => s.includes(passwordChunk2));
110
111 if (matches.length) {
112 isPwned = true
113 }
114 }
115 callback(isPwned)
116 }
117 });
118}
119
120//var checkTimer;
121
122function passUpdated() {
123 var nScore = 0;
124 var message = '';
125
126 var pass = $('#pw1').val();
127 var pass2 = $('#pw2').val();
128
129 //clearTimeout(checkTimer);
130
131 try {
132 if (!pass)
133 throw '';
134
135 if (pass.match(/[^a-zA-Z0-9!@#$%()_+=:;",.?/-]/))
136 throw INVALID_CHARS;
137
138 var nLength = pass.length;
139 if (nLength < 8)
140 throw TOO_SHORT.replace('%%', 8 - nLength);
141 if (nLength > 20)
142 throw TOO_LONG.replace('%%', nLength - 20);
143
144 nScore = 4 * nLength;
145
146 // check for upper-/lowercase, numeric and special chars pattern matches
Marc Kupietz216ff532023-03-10 13:54:23 +0100147 var nAlphaUC = 0,
148 nAlphaLC = 0,
149 nNumber = 0,
150 nSpecial = 0;
151 var nMidChar = 0,
152 nRepChar = 0,
153 nRepInc = 0;
154 var nConsecAlphaUC = 0,
155 nConsecAlphaLC = 0,
156 nConsecNumber = 0;
157 var nTmpAlphaUC = '',
158 nTmpAlphaLC = '',
159 nTmpNumber = '';
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100160 for (var i = 0; i < nLength; i++) {
Marc Kupietz216ff532023-03-10 13:54:23 +0100161 if (pass[i].match(/[A-Z]/g)) { // uppercase characters
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100162 if (nTmpAlphaUC !== '' && (nTmpAlphaUC + 1) == i) {
163 nConsecAlphaUC++;
164 }
165 nTmpAlphaUC = i;
166 nAlphaUC++;
Marc Kupietz216ff532023-03-10 13:54:23 +0100167 } else if (pass[i].match(/[a-z]/g)) { // lowercase characters
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100168 if (nTmpAlphaLC !== '' && (nTmpAlphaLC + 1) == i) {
169 nConsecAlphaLC++;
170 }
171 nTmpAlphaLC = i;
172 nAlphaLC++;
Marc Kupietz216ff532023-03-10 13:54:23 +0100173 } else if (pass[i].match(/[0-9]/g)) { // numbers
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100174 if (i > 0 && i < (nLength - 1)) {
175 nMidChar++;
176 }
177 if (nTmpNumber !== '' && (nTmpNumber + 1) == i) {
178 nConsecNumber++;
179 }
180 nTmpNumber = i;
181 nNumber++;
Marc Kupietz216ff532023-03-10 13:54:23 +0100182 } else { // special characters
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100183 if (i > 0 && i < (nLength - 1)) {
184 nMidChar++;
185 }
186 nSpecial++;
187 }
188
189 // check for repeated characters
190 var bCharExists = false;
191 for (var j = 0; j < nLength; j++) {
192 if (pass[i] == pass[j] && i != j) {
193 bCharExists = true;
194 nRepInc += Math.abs(nLength / (j - i));
195 }
196 }
197 if (bCharExists) {
198 nRepChar++;
199 var nUnqChar = nLength - nRepChar;
200 nRepInc = (nUnqChar) ? Math.ceil(nRepInc / nUnqChar) : Math.ceil(nRepInc);
201 }
202 }
203
204 // check for sequential alpha string patterns (forward and reverse)
205 var sAlphas = "abcdefghijklmnopqrstuvwxyz";
206 var nSeqAlpha = 0;
207 for (var i = 0; i < 23; i++) {
208 var sFwd = sAlphas.substring(i, i + 3);
209 var sRev = sFwd.strReverse();
Marc Kupietz216ff532023-03-10 13:54:23 +0100210 if (pass.toLowerCase().indexOf(sFwd) != -1 ||
211 pass.toLowerCase().indexOf(sRev) != -1)
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100212 nSeqAlpha++;
213 }
214
215 // check for sequential numeric string patterns (forward and reverse)
216 var sNumerics = "01234567890";
217 var nSeqNumber = 0;
218 for (var i = 0; i < 8; i++) {
219 var sFwd = sNumerics.substring(i, i + 3);
220 var sRev = sFwd.strReverse();
Marc Kupietz216ff532023-03-10 13:54:23 +0100221 if (pass.toLowerCase().indexOf(sFwd) != -1 ||
222 pass.toLowerCase().indexOf(sRev) != -1)
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100223 nSeqNumber++;
224 }
225
226 // general point assignment
Marc Kupietz216ff532023-03-10 13:54:23 +0100227 if (nAlphaUC > 0 && nAlphaUC < nLength) // uppercase characters
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100228 nScore += 2 * (nLength - nAlphaUC);
Marc Kupietz216ff532023-03-10 13:54:23 +0100229 if (nAlphaLC > 0 && nAlphaLC < nLength) // lowercase characters
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100230 nScore += 2 * (nLength - nAlphaLC);
Marc Kupietz216ff532023-03-10 13:54:23 +0100231 if (nNumber > 0 && nNumber < nLength) // numbers
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100232 nScore += 2 * nNumber;
Marc Kupietz216ff532023-03-10 13:54:23 +0100233 if (nSpecial > 0) // special characters
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100234 nScore += 4 * nSpecial;
Marc Kupietz216ff532023-03-10 13:54:23 +0100235 if (nMidChar > 0) // mid numbers/special characters
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100236 nScore += 2 * nMidChar;
237
238 // point deductions for poor practices
Marc Kupietz216ff532023-03-10 13:54:23 +0100239 if ((nAlphaLC > 0 || nAlphaUC > 0) &&
240 nSpecial === 0 && nNumber === 0) // characters only
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100241 nScore -= nLength;
Marc Kupietz216ff532023-03-10 13:54:23 +0100242 if (nAlphaLC === 0 && nAlphaUC === 0 &&
243 nSpecial === 0 && nNumber > 0) // numbers only
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100244 nScore -= nLength;
Marc Kupietz216ff532023-03-10 13:54:23 +0100245 if (nRepChar > 0) // same character exists more than once
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100246 nScore -= nRepInc;
Marc Kupietz216ff532023-03-10 13:54:23 +0100247 if (nConsecAlphaUC > 0) // consecutive uppercase letters exist
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100248 nScore -= 2 * nConsecAlphaUC;
Marc Kupietz216ff532023-03-10 13:54:23 +0100249 if (nConsecAlphaLC > 0) // consecutive lowercase letters exist
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100250 nScore -= 2 * nConsecAlphaLC;
Marc Kupietz216ff532023-03-10 13:54:23 +0100251 if (nConsecNumber > 0) // consecutive numbers exist
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100252 nScore -= 2 * nConsecNumber;
Marc Kupietz216ff532023-03-10 13:54:23 +0100253 if (nSeqAlpha > 0) // sequential alpha strings exist (3 chars or more)
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100254 nScore -= 3 * nSeqAlpha;
Marc Kupietz216ff532023-03-10 13:54:23 +0100255 if (nSeqNumber > 0) // sequential numeric strings exist (3 chars or more)
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100256 nScore -= 3 * nSeqNumber;
257
258 // determine if mandatory requirements have been met
259 var arrChars = [nAlphaUC, nAlphaLC, nNumber, nSpecial];
260 var nReqChar = 0;
261 for (var i = 0; i < arrChars.length; i++) {
262 if (arrChars[i]) {
263 nReqChar++;
264 }
265 }
266 if (nReqChar >= arrChars.length)
267 nScore += 2 * nReqChar;
268 else if (nReqChar < arrChars.length - 1)
269 nScore -= 2 * nReqChar;
270
271 // limit points to 3..100
272 nScore = Math.max(3, Math.min(nScore, 100));
273
274 // set message according to points
275 if (nScore >= 80)
276 message = QUAL_STRONG;
277 else if (nScore >= 60)
278 message = QUAL_GOOD;
279 else if (nScore >= 40)
280 message = QUAL_MEDIUM;
281 else if (nScore >= 10)
282 message = QUAL_LOW;
283 else
284 message = QUAL_NONE;
285 } catch (error) {
286 nScore = 3;
287 message = error;
288 }
289 /*
290 if (pass.length > 0 && pass2.length > 0) {
291 message+=' / ';
292 message+=(pass == pass2) ? REP_OK : REP_NE;
293 }
294 */
295 $('#pwqinfo').val(message);
296
297 var progress = $('#pwqbar');
298 progress.width(nScore + '%');
299 progress.attr('aria-valuenow', nScore);
300 if (nScore >= 60)
301 progress.removeClass('bg-danger bg-warning').addClass('bg-success');
302 else if (nScore >= 40)
303 progress.removeClass('bg-danger bg-success').addClass('bg-warning');
304 else
305 progress.removeClass('bg-warning bg-success').addClass('bg-danger');
306
307 if (nScore >= 40) {
308 $("#password-div")[0].classList.add('was-validated');
309 $('#btn_change').prop('disabled', pass !== pass2);
310 $("#pw1")[0].setCustomValidity("");
Marc Kupietz216ff532023-03-10 13:54:23 +0100311 isPasswordPwned(pass, function(isPwned) {
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100312 if (isPwned) {
313 $('#pwqinfo').val(PWNED);
314 progress.removeClass().addClass('low');
315 progress.val(3);
316 $("#pw1")[0].setCustomValidity("Password is pwned");
317 }
318 });
319 } else {
320 $("#password-div")[0].classList.add('was-validated');
321 $('#btn_change').prop('disabled', 1);
322 $("#pw1")[0].setCustomValidity("Password is to week");
323 }
324
Marc Kupietz216ff532023-03-10 13:54:23 +0100325}