blob: 0f0025e09102a90946e8665bd44e1d31f36f7507 [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 }
34 }
Marc Kupietz216ff532023-03-10 13:54:23 +010035 $("#total_due").val(costs.toLocaleString('en-US', { minimumFractionDigits: 2, maximumFractionDigits: 2 }));
Marc Kupietz79c2b922023-03-08 09:29:11 +010036}
37
38function update_paper_id_field() {
39 var paper_id = $("#paper_id");
40 if ($('#author').is(":checked")) {
41 paper_id.removeAttr('disabled');
42 paper_id.attr('placeholder', '0000');
43 paper_id.attr('pattern', '\\d{4}');
44 paper_id.attr('required', 'required');
Marc Kupietz216ff532023-03-10 13:54:23 +010045 //if (paper_id.attr('oldval').match(/^\\d{4}$/)) {
46 // paper_id.val(paper_id.attr('oldval'));
47 //}
Marc Kupietz79c2b922023-03-08 09:29:11 +010048 } else {
Marc Kupietz216ff532023-03-10 13:54:23 +010049 paper_id.attr('oldval', paper_id.val());
Marc Kupietz79c2b922023-03-08 09:29:11 +010050 paper_id.val('');
51 paper_id.attr('disabled', 'disabled');
52 paper_id.removeAttr('placeholder');
53 paper_id.removeAttr('pattern');
54 paper_id.removeAttr('required');
55 }
56}
Marc Kupietz484ec8e2023-02-25 11:23:07 +010057
58function check_password_match() {
59 pass = $("#pw1").val();
60 pass2 = $("#pw2").val();
61
62 if (pass2.length > 0) {
63 $("#pwconfirm")[0].classList.add('was-validated')
64 if (pass != pass2) {
65 $("#pw2")[0].setCustomValidity("Passwords do not match");
66 } else {
67 $("#pw2")[0].setCustomValidity(""); // is valid
68 }
69 } else {
70 $("#pwconfirm")[0].classList.remove('was-validated')
71 }
72 passUpdated();
73}
74
75TOO_SHORT = 'Password too short, still %% characters needed';
76TOO_LONG = 'Password too long, please remove %% characters';
77INVALID_CHARS = 'Password contains invalid characters';
78QUAL_NONE = 'Password is very weak'
79QUAL_LOW = 'Password is weak';
80QUAL_MEDIUM = 'Password is average'
81QUAL_GOOD = 'Password is good';
82QUAL_STRONG = 'Password is strong';
83REP_OK = 'Repetition ok';
84REP_NE = 'Passwords not identical';
85PWNED = 'Password found in public password list';
86
Marc Kupietz216ff532023-03-10 13:54:23 +010087String.prototype.strReverse = function() {
Marc Kupietz484ec8e2023-02-25 11:23:07 +010088 var newstring = '';
89 for (var s = 0; s < this.length; s++)
90 newstring = this.charAt(s) + newstring;
91 return newstring;
92};
93
94function isPasswordPwned(pass, callback) {
95 const sha1Hash = CryptoJS.enc.Hex.stringify(CryptoJS.SHA1(pass)).toUpperCase();
96 const passwordChunk1 = sha1Hash.substring(0, 5);
97 const passwordChunk2 = sha1Hash.substring(5);
Marc Kupietz216ff532023-03-10 13:54:23 +010098 $.get('https://api.pwnedpasswords.com/range/' + passwordChunk1).done(function(data, status) {
Marc Kupietz484ec8e2023-02-25 11:23:07 +010099 var isPwned = false;
100 if (status == "success") {
101 if (data && data.length) {
102 const chunks = data.split('\r\n');
103 const matches = chunks.filter(s => s.includes(passwordChunk2));
104
105 if (matches.length) {
106 isPwned = true
107 }
108 }
109 callback(isPwned)
110 }
111 });
112}
113
114//var checkTimer;
115
116function passUpdated() {
117 var nScore = 0;
118 var message = '';
119
120 var pass = $('#pw1').val();
121 var pass2 = $('#pw2').val();
122
123 //clearTimeout(checkTimer);
124
125 try {
126 if (!pass)
127 throw '';
128
129 if (pass.match(/[^a-zA-Z0-9!@#$%()_+=:;",.?/-]/))
130 throw INVALID_CHARS;
131
132 var nLength = pass.length;
133 if (nLength < 8)
134 throw TOO_SHORT.replace('%%', 8 - nLength);
135 if (nLength > 20)
136 throw TOO_LONG.replace('%%', nLength - 20);
137
138 nScore = 4 * nLength;
139
140 // check for upper-/lowercase, numeric and special chars pattern matches
Marc Kupietz216ff532023-03-10 13:54:23 +0100141 var nAlphaUC = 0,
142 nAlphaLC = 0,
143 nNumber = 0,
144 nSpecial = 0;
145 var nMidChar = 0,
146 nRepChar = 0,
147 nRepInc = 0;
148 var nConsecAlphaUC = 0,
149 nConsecAlphaLC = 0,
150 nConsecNumber = 0;
151 var nTmpAlphaUC = '',
152 nTmpAlphaLC = '',
153 nTmpNumber = '';
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100154 for (var i = 0; i < nLength; i++) {
Marc Kupietz216ff532023-03-10 13:54:23 +0100155 if (pass[i].match(/[A-Z]/g)) { // uppercase characters
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100156 if (nTmpAlphaUC !== '' && (nTmpAlphaUC + 1) == i) {
157 nConsecAlphaUC++;
158 }
159 nTmpAlphaUC = i;
160 nAlphaUC++;
Marc Kupietz216ff532023-03-10 13:54:23 +0100161 } else if (pass[i].match(/[a-z]/g)) { // lowercase characters
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100162 if (nTmpAlphaLC !== '' && (nTmpAlphaLC + 1) == i) {
163 nConsecAlphaLC++;
164 }
165 nTmpAlphaLC = i;
166 nAlphaLC++;
Marc Kupietz216ff532023-03-10 13:54:23 +0100167 } else if (pass[i].match(/[0-9]/g)) { // numbers
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100168 if (i > 0 && i < (nLength - 1)) {
169 nMidChar++;
170 }
171 if (nTmpNumber !== '' && (nTmpNumber + 1) == i) {
172 nConsecNumber++;
173 }
174 nTmpNumber = i;
175 nNumber++;
Marc Kupietz216ff532023-03-10 13:54:23 +0100176 } else { // special characters
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100177 if (i > 0 && i < (nLength - 1)) {
178 nMidChar++;
179 }
180 nSpecial++;
181 }
182
183 // check for repeated characters
184 var bCharExists = false;
185 for (var j = 0; j < nLength; j++) {
186 if (pass[i] == pass[j] && i != j) {
187 bCharExists = true;
188 nRepInc += Math.abs(nLength / (j - i));
189 }
190 }
191 if (bCharExists) {
192 nRepChar++;
193 var nUnqChar = nLength - nRepChar;
194 nRepInc = (nUnqChar) ? Math.ceil(nRepInc / nUnqChar) : Math.ceil(nRepInc);
195 }
196 }
197
198 // check for sequential alpha string patterns (forward and reverse)
199 var sAlphas = "abcdefghijklmnopqrstuvwxyz";
200 var nSeqAlpha = 0;
201 for (var i = 0; i < 23; i++) {
202 var sFwd = sAlphas.substring(i, i + 3);
203 var sRev = sFwd.strReverse();
Marc Kupietz216ff532023-03-10 13:54:23 +0100204 if (pass.toLowerCase().indexOf(sFwd) != -1 ||
205 pass.toLowerCase().indexOf(sRev) != -1)
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100206 nSeqAlpha++;
207 }
208
209 // check for sequential numeric string patterns (forward and reverse)
210 var sNumerics = "01234567890";
211 var nSeqNumber = 0;
212 for (var i = 0; i < 8; i++) {
213 var sFwd = sNumerics.substring(i, i + 3);
214 var sRev = sFwd.strReverse();
Marc Kupietz216ff532023-03-10 13:54:23 +0100215 if (pass.toLowerCase().indexOf(sFwd) != -1 ||
216 pass.toLowerCase().indexOf(sRev) != -1)
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100217 nSeqNumber++;
218 }
219
220 // general point assignment
Marc Kupietz216ff532023-03-10 13:54:23 +0100221 if (nAlphaUC > 0 && nAlphaUC < nLength) // uppercase characters
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100222 nScore += 2 * (nLength - nAlphaUC);
Marc Kupietz216ff532023-03-10 13:54:23 +0100223 if (nAlphaLC > 0 && nAlphaLC < nLength) // lowercase characters
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100224 nScore += 2 * (nLength - nAlphaLC);
Marc Kupietz216ff532023-03-10 13:54:23 +0100225 if (nNumber > 0 && nNumber < nLength) // numbers
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100226 nScore += 2 * nNumber;
Marc Kupietz216ff532023-03-10 13:54:23 +0100227 if (nSpecial > 0) // special characters
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100228 nScore += 4 * nSpecial;
Marc Kupietz216ff532023-03-10 13:54:23 +0100229 if (nMidChar > 0) // mid numbers/special characters
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100230 nScore += 2 * nMidChar;
231
232 // point deductions for poor practices
Marc Kupietz216ff532023-03-10 13:54:23 +0100233 if ((nAlphaLC > 0 || nAlphaUC > 0) &&
234 nSpecial === 0 && nNumber === 0) // characters only
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100235 nScore -= nLength;
Marc Kupietz216ff532023-03-10 13:54:23 +0100236 if (nAlphaLC === 0 && nAlphaUC === 0 &&
237 nSpecial === 0 && nNumber > 0) // numbers only
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100238 nScore -= nLength;
Marc Kupietz216ff532023-03-10 13:54:23 +0100239 if (nRepChar > 0) // same character exists more than once
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100240 nScore -= nRepInc;
Marc Kupietz216ff532023-03-10 13:54:23 +0100241 if (nConsecAlphaUC > 0) // consecutive uppercase letters exist
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100242 nScore -= 2 * nConsecAlphaUC;
Marc Kupietz216ff532023-03-10 13:54:23 +0100243 if (nConsecAlphaLC > 0) // consecutive lowercase letters exist
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100244 nScore -= 2 * nConsecAlphaLC;
Marc Kupietz216ff532023-03-10 13:54:23 +0100245 if (nConsecNumber > 0) // consecutive numbers exist
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100246 nScore -= 2 * nConsecNumber;
Marc Kupietz216ff532023-03-10 13:54:23 +0100247 if (nSeqAlpha > 0) // sequential alpha strings exist (3 chars or more)
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100248 nScore -= 3 * nSeqAlpha;
Marc Kupietz216ff532023-03-10 13:54:23 +0100249 if (nSeqNumber > 0) // sequential numeric strings exist (3 chars or more)
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100250 nScore -= 3 * nSeqNumber;
251
252 // determine if mandatory requirements have been met
253 var arrChars = [nAlphaUC, nAlphaLC, nNumber, nSpecial];
254 var nReqChar = 0;
255 for (var i = 0; i < arrChars.length; i++) {
256 if (arrChars[i]) {
257 nReqChar++;
258 }
259 }
260 if (nReqChar >= arrChars.length)
261 nScore += 2 * nReqChar;
262 else if (nReqChar < arrChars.length - 1)
263 nScore -= 2 * nReqChar;
264
265 // limit points to 3..100
266 nScore = Math.max(3, Math.min(nScore, 100));
267
268 // set message according to points
269 if (nScore >= 80)
270 message = QUAL_STRONG;
271 else if (nScore >= 60)
272 message = QUAL_GOOD;
273 else if (nScore >= 40)
274 message = QUAL_MEDIUM;
275 else if (nScore >= 10)
276 message = QUAL_LOW;
277 else
278 message = QUAL_NONE;
279 } catch (error) {
280 nScore = 3;
281 message = error;
282 }
283 /*
284 if (pass.length > 0 && pass2.length > 0) {
285 message+=' / ';
286 message+=(pass == pass2) ? REP_OK : REP_NE;
287 }
288 */
289 $('#pwqinfo').val(message);
290
291 var progress = $('#pwqbar');
292 progress.width(nScore + '%');
293 progress.attr('aria-valuenow', nScore);
294 if (nScore >= 60)
295 progress.removeClass('bg-danger bg-warning').addClass('bg-success');
296 else if (nScore >= 40)
297 progress.removeClass('bg-danger bg-success').addClass('bg-warning');
298 else
299 progress.removeClass('bg-warning bg-success').addClass('bg-danger');
300
301 if (nScore >= 40) {
302 $("#password-div")[0].classList.add('was-validated');
303 $('#btn_change').prop('disabled', pass !== pass2);
304 $("#pw1")[0].setCustomValidity("");
Marc Kupietz216ff532023-03-10 13:54:23 +0100305 isPasswordPwned(pass, function(isPwned) {
Marc Kupietz484ec8e2023-02-25 11:23:07 +0100306 if (isPwned) {
307 $('#pwqinfo').val(PWNED);
308 progress.removeClass().addClass('low');
309 progress.val(3);
310 $("#pw1")[0].setCustomValidity("Password is pwned");
311 }
312 });
313 } else {
314 $("#password-div")[0].classList.add('was-validated');
315 $('#btn_change').prop('disabled', 1);
316 $("#pw1")[0].setCustomValidity("Password is to week");
317 }
318
Marc Kupietz216ff532023-03-10 13:54:23 +0100319}