1
// $Id: user.js,v 1.6 2007/09/12 18:29:32 goba Exp $
4
* Attach handlers to evaluate the strength of any password fields and to check
5
* that its confirmation is correct.
7
Drupal.behaviors.password = function(context) {
8
var translate = Drupal.settings.password;
9
$("input.password-field:not(.password-processed)", context).each(function() {
10
var passwordInput = $(this).addClass('password-processed');
11
var parent = $(this).parent();
12
// Wait this number of milliseconds before checking password.
13
var monitorDelay = 700;
15
// Add the password strength layers.
16
$(this).after('<span class="password-strength"><span class="password-title">'+ translate.strengthTitle +'</span> <span class="password-result"></span></span>').parent();
17
var passwordStrength = $("span.password-strength", parent);
18
var passwordResult = $("span.password-result", passwordStrength);
19
parent.addClass("password-parent");
21
// Add the password confirmation layer.
22
var outerItem = $(this).parent().parent();
23
$("input.password-confirm", outerItem).after('<span class="password-confirm">'+ translate["confirmTitle"] +' <span></span></span>').parent().addClass("confirm-parent");
24
var confirmInput = $("input.password-confirm", outerItem);
25
var confirmResult = $("span.password-confirm", outerItem);
26
var confirmChild = $("span", confirmResult);
28
// Add the description box at the end.
29
$(confirmInput).parent().after('<div class="password-description"></div>');
30
var passwordDescription = $("div.password-description", $(this).parent().parent()).hide();
32
// Check the password fields.
33
var passwordCheck = function () {
34
// Remove timers for a delayed check if they exist.
36
clearTimeout(this.timer);
39
// Verify that there is a password to check.
40
if (!passwordInput.val()) {
41
passwordStrength.css({ visibility: "hidden" });
42
passwordDescription.hide();
46
// Evaluate password strength.
48
var result = Drupal.evaluatePasswordStrength(passwordInput.val());
49
passwordResult.html(result.strength == "" ? "" : translate[result.strength +"Strength"]);
51
// Map the password strength to the relevant drupal CSS class.
52
var classMap = { low: "error", medium: "warning", high: "ok" };
53
var newClass = classMap[result.strength] || "";
55
// Remove the previous styling if any exists; add the new class.
56
if (this.passwordClass) {
57
passwordResult.removeClass(this.passwordClass);
58
passwordDescription.removeClass(this.passwordClass);
60
passwordDescription.html(result.message);
61
passwordResult.addClass(newClass);
62
if (result.strength == "high") {
63
passwordDescription.hide();
66
passwordDescription.addClass(newClass);
68
this.passwordClass = newClass;
70
// Check that password and confirmation match.
72
// Hide the result layer if confirmation is empty, otherwise show the layer.
73
confirmResult.css({ visibility: (confirmInput.val() == "" ? "hidden" : "visible") });
75
var success = passwordInput.val() == confirmInput.val();
77
// Remove the previous styling if any exists.
78
if (this.confirmClass) {
79
confirmChild.removeClass(this.confirmClass);
82
// Fill in the correct message and set the class accordingly.
83
var confirmClass = success ? "ok" : "error";
84
confirmChild.html(translate["confirm"+ (success ? "Success" : "Failure")]).addClass(confirmClass);
85
this.confirmClass = confirmClass;
87
// Show the indicator and tips.
88
passwordStrength.css({ visibility: "visible" });
89
passwordDescription.show();
92
// Do a delayed check on the password fields.
93
var passwordDelayedCheck = function() {
94
// Postpone the check since the user is most likely still typing.
96
clearTimeout(this.timer);
99
// When the user clears the field, hide the tips immediately.
100
if (!passwordInput.val()) {
101
passwordStrength.css({ visibility: "hidden" });
102
passwordDescription.hide();
106
// Schedule the actual check.
107
this.timer = setTimeout(passwordCheck, monitorDelay);
109
// Monitor keyup and blur events.
110
// Blur must be used because a mouse paste does not trigger keyup.
111
passwordInput.keyup(passwordDelayedCheck).blur(passwordCheck);
112
confirmInput.keyup(passwordDelayedCheck).blur(passwordCheck);
117
* Evaluate the strength of a user's password.
119
* Returns the estimated strength and the relevant output message.
121
Drupal.evaluatePasswordStrength = function(value) {
122
var strength = "", msg = "", translate = Drupal.settings.password;
124
var hasLetters = value.match(/[a-zA-Z]+/);
125
var hasNumbers = value.match(/[0-9]+/);
126
var hasPunctuation = value.match(/[^a-zA-Z0-9]+/);
127
var hasCasing = value.match(/[a-z]+.*[A-Z]+|[A-Z]+.*[a-z]+/);
129
// Check if the password is blank.
134
// Check if length is less than 6 characters.
135
else if (value.length < 6) {
137
msg = translate.tooShort;
139
// Check if password is the same as the username (convert both to lowercase).
140
else if (value.toLowerCase() == translate.username.toLowerCase()) {
142
msg = translate.sameAsUsername;
144
// Check if it contains letters, numbers, punctuation, and upper/lower case.
145
else if (hasLetters && hasNumbers && hasPunctuation && hasCasing) {
148
// Password is not secure enough so construct the medium-strength message.
150
// Extremely bad passwords still count as low.
151
var count = (hasLetters ? 1 : 0) + (hasNumbers ? 1 : 0) + (hasPunctuation ? 1 : 0) + (hasCasing ? 1 : 0);
152
strength = count > 1 ? "medium" : "low";
155
if (!hasLetters || !hasCasing) {
156
msg.push(translate.addLetters);
159
msg.push(translate.addNumbers);
161
if (!hasPunctuation) {
162
msg.push(translate.addPunctuation);
164
msg = translate.needsMoreVariation +"<ul><li>"+ msg.join("</li><li>") +"</li></ul>";
167
return { strength: strength, message: msg };
171
* Set the client's system timezone as default values of form fields.
173
Drupal.setDefaultTimezone = function() {
174
var offset = new Date().getTimezoneOffset() * -60;
175
$("#edit-date-default-timezone, #edit-user-register-timezone").val(offset);
179
* On the admin/user/settings page, conditionally show all of the
180
* picture-related form elements depending on the current value of the
181
* "Picture support" radio buttons.
183
Drupal.behaviors.userSettings = function (context) {
184
$('div.user-admin-picture-radios input[type=radio]:not(.userSettings-processed)', context).addClass('userSettings-processed').click(function () {
185
$('div.user-admin-picture-settings', context)[['hide', 'show'][this.value]]();