From 707142a5e6cea0d63e288a9c3c458be3e500ec62 Mon Sep 17 00:00:00 2001 From: Sue Ann Kim <38238542+wackyleo459@users.noreply.github.com> Date: Tue, 23 Aug 2022 00:22:05 -0700 Subject: [PATCH] Device Alias form has validation factors: (#813) * Device Alias form validation has validation factors: - Max length of 30 characters - Name can not start with a space - Name should not contain special characters (only alphanumeric characters, underscore character, and hyphen or space in the middle are allowed) * Lint Formatting * Regex pattern explained. Better formatting and js logic --- .../addDevice/manage/addDevicePickAlias.ts | 20 +++++++++++++ .../src/flows/addDevice/validateAlias.ts | 25 ++++++++++++++++ src/frontend/src/flows/register.ts | 29 ++++++++++++++++++- 3 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 src/frontend/src/flows/addDevice/validateAlias.ts diff --git a/src/frontend/src/flows/addDevice/manage/addDevicePickAlias.ts b/src/frontend/src/flows/addDevice/manage/addDevicePickAlias.ts index 041dd4e739..d50293f45c 100644 --- a/src/frontend/src/flows/addDevice/manage/addDevicePickAlias.ts +++ b/src/frontend/src/flows/addDevice/manage/addDevicePickAlias.ts @@ -1,5 +1,10 @@ import { html, render } from "lit-html"; import { initLogout, logoutSection } from "../../../components/logout"; +import { validateAlias } from "../validateAlias"; +// Regex Pattern for input: All characters, must be alphabet or number +// Can have hyphen(s), space(s) or underscore(s) in the middle. +// Good examples: "2019_macbook", "2019-Macbook", "2019 Macbook" +// Bad examples: "2019 macbook!", "2010 macbook_", "space trails at end " const pageContent = () => html`
@@ -13,6 +18,9 @@ const pageContent = () => html` placeholder="Device alias" type="text" required + maxlength="30" + pattern="^[A-Za-z0-9]+((-|\\s|_)*[A-Za-z0-9])*$" + spellcheck="false" /> @@ -101,6 +111,23 @@ const init = (connection: Connection): Promise => reject(err); } }; + + const registerAlias = document.getElementById( + "registerAlias" + ) as HTMLInputElement; + + registerAlias.addEventListener("invalid", () => { + const message = validateAlias( + registerAlias.validity, + registerAlias.value + ); + registerAlias.setCustomValidity(message); + }); + + registerAlias.addEventListener("input", () => { + registerAlias.setCustomValidity(""); + registerAlias.reportValidity(); + }); }); const tick = (): Promise => new Promise((resolve) => nextTick(resolve));