From 048824f08e5c23876bd8c0cadaaceca3cd727834 Mon Sep 17 00:00:00 2001 From: olehp Date: Wed, 17 Apr 2024 17:23:10 +0300 Subject: [PATCH] unify password validation --- src/components/Main/Authentication/Registration/index.tsx | 6 +++--- src/utils/isAlphanumeric.ts | 5 +++++ src/utils/isPasswordFormatValid.ts | 7 ------- 3 files changed, 8 insertions(+), 10 deletions(-) create mode 100644 src/utils/isAlphanumeric.ts delete mode 100644 src/utils/isPasswordFormatValid.ts diff --git a/src/components/Main/Authentication/Registration/index.tsx b/src/components/Main/Authentication/Registration/index.tsx index 6d4a1ff7d..b73bb1d0f 100644 --- a/src/components/Main/Authentication/Registration/index.tsx +++ b/src/components/Main/Authentication/Registration/index.tsx @@ -1,7 +1,7 @@ import { KeyboardEvent, useEffect, useState } from "react"; import { Controller, useForm } from "react-hook-form"; import { sendUserActionTrackingEvent } from "../../../../utils/actions/sendUserActionTrackingEvent"; -import { isValidPasswordFormat } from "../../../../utils/isPasswordFormatValid"; +import { isAlphanumeric } from "../../../../utils/isAlphanumeric"; import { isValidEmailFormat } from "../../../../utils/isValidEmailFormat"; import { TextField } from "../../../common/RegistrationDialog/TextField"; import { LockIcon } from "../../../common/icons/12px/LockIcon"; @@ -31,8 +31,8 @@ const validatePassword = (password: string): string | boolean => { return "Password must be at least 6."; } - if (!isValidPasswordFormat(password)) { - return "Password must contain one special character !@#$%^&*-"; + if (isAlphanumeric(password)) { + return "Password must contain one special character"; } return true; diff --git a/src/utils/isAlphanumeric.ts b/src/utils/isAlphanumeric.ts new file mode 100644 index 000000000..e1a580aa3 --- /dev/null +++ b/src/utils/isAlphanumeric.ts @@ -0,0 +1,5 @@ +const PASSWORD_ALPHANUMERIC_REGEX = /^[a-zA-Z0-9]*$/gm; + +export const isAlphanumeric = (password: string): boolean => { + return new RegExp(PASSWORD_ALPHANUMERIC_REGEX).test(password); +}; diff --git a/src/utils/isPasswordFormatValid.ts b/src/utils/isPasswordFormatValid.ts deleted file mode 100644 index f5aed159b..000000000 --- a/src/utils/isPasswordFormatValid.ts +++ /dev/null @@ -1,7 +0,0 @@ -// password should have at least 6 symbols and could be alphanumeric with at least one spec symbol -const PASSWORD_REGEX = - /^(?=.*[A-Za-z0-9])(?=.*[!@#$%^&*-])[A-Za-z0-9!@#$%^&*-]{6,}$/gm; - -export const isValidPasswordFormat = (password: string): boolean => { - return new RegExp(PASSWORD_REGEX).test(password); -};