tazpanel view lib/user.js @ rev 340
Rude fix '%0' bug in file editor (still FIXME: editor combines several spaces into one)
author | Aleksej Bobylev <al.bobylev@gmail.com> |
---|---|
date | Thu Jan 24 13:45:23 2013 +0000 (2013-01-24) |
parents | 6f39fe45dc31 |
children | 972b3169b3e4 |
line source
1 /*
2 Login and password validation
3 Copyright (C) 2012 SliTaz GNU/linux - GNU gpl v3
4 */
6 ////
7 // i18n for this javascript
9 function i18n(text){
10 var lang = document.getElementsByTagName("html")[0].getAttribute("lang");
11 var orig = ["Too short!", "Too long!", "Invalid chars!", "(No Password!)", "(Strong)", "(Medium!)", "(Weak!)", "Do Not Match!"];
12 var translate = ["es", "fr", "pt", "ru"];
13 translate['ru'] = ["Слишком короткий!", "Слишком длинный!", "Недопустимые символы!", "(Нет пароля!)", "(Сильный)", "(Средний!)", "(Слабый!)", "Не совпадает!"];
14 translate['fr'] = [ ];
16 var output = text;
17 for (var i=0; i<orig.length; i++) {
18 if (translate[lang] !== undefined && orig[i] == text) {
19 var transTry = translate[lang][i];
20 if (transTry !== undefined && transTry !== '') {
21 var output = transTry;
22 }
23 break
24 }
25 }
26 return(output);
27 }
29 ////
30 // Login validation - typical use:
31 // <input id="login1" onkeyup="checkLogin('login1','msg1'); return false;" />
32 // <span id="msg1"></span>
34 function checkLogin(user,message){
35 var login = document.getElementById(user);
36 var msg = document.getElementById(message);
37 var enoughRegex = new RegExp("(?=.{3,}).*", "g");
38 var incharRegex = new RegExp("^[A-Za-z0-9_-]{3,32}$");
39 // html fragments
40 var nok='<span class="msg-nok">✖ ';
41 var s='</span>';
43 if (login.value == '') {
44 msg.innerHTML = '';
45 } else if (false == enoughRegex.test(login.value)) {
46 msg.innerHTML = nok + i18n('Too short!') + s;
47 return false;
48 } else if (login.value.length > 32) {
49 msg.innerHTML = nok + i18n('Too long!') + s;
50 return false;
51 } else if (false == incharRegex.test(login.value)) {
52 msg.innerHTML = nok + i18n('Invalid chars!') + s;
53 return false;
54 } else {
55 msg.innerHTML = '<span class="msg-ok">✔'+s;
56 }
57 }
59 ////
60 // Password validation - typical use:
61 // <input type="password" id="pass1" onkeyup="checkPwd('pass1','pass2','msg2'); return false;" />
62 // <input type="password" id="pass2" onkeyup="checkPwd('pass1','pass2','msg2'); return false;" />
63 // <span id="msg2"></span>
65 function checkPwd(password,confirm,message){
66 var pwd1 = document.getElementById(password);
67 var pwd2 = document.getElementById(confirm);
68 var msg = document.getElementById(message);
69 // html fragments
70 var nok = '<span class="msg-nok">✖ ';
71 var okw = '<span class="msg-ok">✔ </span><span class="msg-warn">';
72 var s = '</span>';
74 if(pwd1.value == pwd2.value){
75 // passwords match.
76 pwd2.classList.remove('alert');
77 // various checks
78 var enoughRegex = new RegExp("(?=.{3,}).*", "g");
79 var incharRegex = new RegExp("^[A-Za-z0-9!@#$%^&*()_]{3,40}$");
80 var strongRegex = new RegExp("^(?=.{8,})(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*\\W).*$", "g");
81 var mediumRegex = new RegExp("^(?=.{7,})(((?=.*[A-Z])(?=.*[a-z]))|((?=.*[A-Z])(?=.*[0-9]))|((?=.*[a-z])(?=.*[0-9]))).*$", "g");
82 if (pwd1.value.length==0) {
83 msg.innerHTML = okw + i18n('(No Password!)') + s;
84 } else if (pwd1.value.length > 40) {
85 msg.innerHTML = nok + i18n('Too long!') + s;
86 return false;
87 } else if (false == enoughRegex.test(pwd1.value)) {
88 msg.innerHTML = nok + i18n('Too short!') + s;
89 return false;
90 } else if (false == incharRegex.test(pwd1.value)) {
91 msg.innerHTML = nok + i18n('Invalid chars!') + s;
92 return false;
93 } else if (strongRegex.test(pwd1.value)) {
94 msg.innerHTML = '<span class="msg-ok">✔ ' + i18n('(Strong)') + s;
95 } else if (mediumRegex.test(pwd1.value)) {
96 msg.innerHTML = okw + i18n('(Medium!)') + s;
97 } else {
98 msg.innerHTML = okw + i18n('(Weak!)') + s;
99 }
100 } else {
101 // passwords do not match.
102 pwd2.classList.add('alert');
103 msg.innerHTML = nok + i18n('Do Not Match!') + s;
104 return false;
105 }
106 }