Skip to content

Commit e6fc266

Browse files
author
Yeray Diaz Diaz
committed
More complex validation of registration form toggling the submit
button accordingly. Checks presence of all required values and verifies the zxcvbn score is > 2
1 parent 17eb7be commit e6fc266

File tree

3 files changed

+48
-4
lines changed

3 files changed

+48
-4
lines changed

warehouse/static/js/warehouse/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ docReady(Analytics);
6262

6363
// Handle the JS based automatic form submission.
6464
docReady(formUtils.submitTriggers);
65-
docReady(formUtils.passwordStrength);
65+
docReady(formUtils.registrationFormValidation);
6666

6767
docReady(Statuspage);
6868

warehouse/static/js/warehouse/utils/forms.js

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,21 +52,65 @@ export function submitTriggers() {
5252

5353
/* global zxcvbn */
5454

55+
const disableSubmitButton = () => {
56+
let submitBtn = document.querySelector("#submit");
57+
submitBtn.setAttribute("disabled", "disabled");
58+
};
59+
60+
const enableSubmitButton = () => {
61+
let submitBtn = document.querySelector("#submit");
62+
submitBtn.removeAttribute("disabled");
63+
};
64+
5565
const checkPasswordStrength = (event) => {
5666
let result = document.querySelector(".pw-strength-guage");
5767
if (event.target.value === "") {
5868
result.setAttribute("class", "pw-strength-guage");
69+
result.setAttribute("score", 0);
5970
} else {
6071
// following recommendations on the zxcvbn JS docs
6172
// the zxcvbn function is available by loading `vendor/zxcvbn.js`
6273
// in the register page template only
6374
let zxcvbnResult = zxcvbn(event.target.value);
6475
result.setAttribute("class", `pw-strength-guage pw-strength-guage__${zxcvbnResult.score}`);
76+
result.setAttribute("score", zxcvbnResult.score);
77+
}
78+
};
79+
80+
const currentZxcvbnScore = () => {
81+
let result = document.querySelector(".pw-strength-guage");
82+
return parseInt(result.getAttribute("score"));
83+
};
84+
85+
const passwordsMatch = () => {
86+
let password = document.querySelector("#password");
87+
let passwordConfirmation = document.querySelector("#password_confirm");
88+
return password.value === passwordConfirmation.value;
89+
};
90+
91+
const requiredFieldsHaveValues = () => {
92+
let inputFields = document.querySelectorAll("#content input");
93+
let inputFieldsArray = Array.prototype.slice.call(inputFields);
94+
return inputFieldsArray.every(inputField => inputField.value !== "");
95+
};
96+
97+
const checkRequiredFields = () => {
98+
if (requiredFieldsHaveValues() && currentZxcvbnScore() >= 2 && passwordsMatch()) {
99+
enableSubmitButton();
100+
} else {
101+
disableSubmitButton();
65102
}
66103
};
67104

68-
export function passwordStrength() {
105+
export function registrationFormValidation() {
69106
let password = document.querySelector("#password");
70107
if (password === null) return;
108+
disableSubmitButton();
109+
let inputFields = document.querySelectorAll("#content input");
110+
inputFields.forEach(input => {
111+
if (input.hasAttribute("required")) {
112+
input.addEventListener("input", checkRequiredFields, false);
113+
}
114+
});
71115
password.addEventListener("input", checkPasswordStrength, false);
72116
}

warehouse/templates/accounts/register.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ <h2>Create an account on PyPI</h2>
3636

3737
<div class="form-group">
3838
<label for="full_name" class="form-group__label">Name</label>
39-
{{ form.full_name(placeholder="Your name", class_="form-group__input") }}
39+
{{ form.full_name(placeholder="Your name", required="required", class_="form-group__input") }}
4040
{% if form.full_name.errors %}
4141
<ul class="form-errors">
4242
{% for error in form.full_name.errors %}
@@ -103,7 +103,7 @@ <h2>Create an account on PyPI</h2>
103103
{{ recaptcha_html(request, form) }}
104104
</div>
105105

106-
<input type="submit" value="Create Account" class="button button--highlight">
106+
<input type="submit" value="Create Account" id="submit" class="button button--highlight">
107107
</form>
108108
</div>
109109
</section>

0 commit comments

Comments
 (0)