Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Device Alias form has 3 validation factors: #813

Merged
merged 9 commits into from
Aug 23, 2022
33 changes: 33 additions & 0 deletions src/frontend/src/flows/addDevice/manage/addDevicePickAlias.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { html, render } from "lit-html";
import { initLogout, logoutSection } from "../../../components/logout";
// Regex Pattern for input: All characters, including starting(^) single character or more(+),
wackyleo459 marked this conversation as resolved.
Show resolved Hide resolved
// must be alphabet, number or underscore. Can have hyphen(s) or space(s) in the middle.
wackyleo459 marked this conversation as resolved.
Show resolved Hide resolved
// Good examples: "2019_macbook", "2019-Macbook", "_2019 Macbook"
// Bad examples: "2019 macbook!", " 2010: macbook", "space trails at end "

const pageContent = () => html`
<div class="container">
Expand All @@ -13,6 +17,9 @@ const pageContent = () => html`
placeholder="Device alias"
type="text"
required
maxlength="30"
pattern="^[A-Za-z0-9_]+((-|\\s)*[A-Za-z0-9_])*$"
wackyleo459 marked this conversation as resolved.
Show resolved Hide resolved
spellcheck="false"
/>
<button type="submit" id="deviceAliasContinue" class="primary">
Add Device
Expand Down Expand Up @@ -51,4 +58,30 @@ const init = (): Promise<string | null> =>
event.preventDefault();
resolve(deviceAlias.value);
};
const deviceInput = document.getElementById(
"deviceAlias"
) as HTMLInputElement;

deviceInput.addEventListener("invalid", () => {
let message = "";
if (deviceInput.validity.valueMissing) {
message = "Please fill out this field.";
} else if (deviceInput.validity.patternMismatch) {
if (deviceInput.value.startsWith(" ")) {
message = "Name can't start with a space.";
} else if (
deviceInput.value.endsWith(" ") ||
deviceInput.value.endsWith("-")
) {
message = "Name can't end with a space or hyphen.";
} else {
message = "Name can't contain special characters.";
}
wackyleo459 marked this conversation as resolved.
Show resolved Hide resolved
}
deviceInput.setCustomValidity(message);
});
deviceInput.addEventListener("input", () => {
deviceInput.setCustomValidity("");
deviceInput.reportValidity();
});
});
40 changes: 39 additions & 1 deletion src/frontend/src/flows/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,16 @@ const pageContent = html`
<h1>Create a new Internet Identity Anchor</h1>
<form id="registerForm">
<p>Please provide a name for your device.</p>
<input id="registerAlias" placeholder="Device name" />
<input
id="registerAlias"
placeholder="Device name"
aria-label="device name"
type="text"
required
maxlength="30"
pattern="^[A-Za-z0-9_]+((-|\\s)*[A-Za-z0-9_])*$"
spellcheck="false"
/>
<button type="submit" class="primary">Create</button>
<button id="registerCancel" type="button">Cancel</button>
</form>
Expand Down Expand Up @@ -101,6 +110,35 @@ const init = (connection: Connection): Promise<LoginFlowResult | null> =>
reject(err);
}
};

const registerAlias = document.getElementById(
"registerAlias"
) as HTMLInputElement;

registerAlias.addEventListener("invalid", () => {
if (registerAlias.validity.valueMissing) {
registerAlias.setCustomValidity("Please fill out this field.");
} else if (registerAlias.validity.patternMismatch) {
if (registerAlias.value.startsWith(" ")) {
registerAlias.setCustomValidity("Name can't start with a space.");
} else if (
registerAlias.value.endsWith(" ") ||
registerAlias.value.endsWith("-")
) {
registerAlias.setCustomValidity(
"Name can't end with a space or hyphen."
);
} else {
registerAlias.setCustomValidity(
"Name can't contain special characters."
);
}
}
});
registerAlias.addEventListener("input", () => {
registerAlias.setCustomValidity("");
registerAlias.reportValidity();
});
});

const tick = (): Promise<void> => new Promise((resolve) => nextTick(resolve));