From f5847c17b58daca00f59a453bedb9da9ecdbd7b2 Mon Sep 17 00:00:00 2001 From: Ajay <97356092+iajaymk@users.noreply.github.com> Date: Tue, 28 Nov 2023 11:44:43 +0530 Subject: [PATCH] fix: only numbers allowed in year of birth (#2541) --- frontend/app/components/profile/component.ts | 17 +++++++++++++++++ frontend/app/components/profile/template.hbs | 1 + .../components/registration-form/component.ts | 18 ++++++++++++++++++ .../components/registration-form/template.hbs | 1 + 4 files changed, 37 insertions(+) diff --git a/frontend/app/components/profile/component.ts b/frontend/app/components/profile/component.ts index 61b822394..de1adcf1e 100644 --- a/frontend/app/components/profile/component.ts +++ b/frontend/app/components/profile/component.ts @@ -117,4 +117,21 @@ export default class ProfileComponent extends Component { }); } } + + @action + setBirthday(e: Event) { + const key = e.key; + const allowedKeys = [ + 'Backspace', + 'Delete', + 'Tab', + 'Enter', + 'ArrowLeft', + 'ArrowRight', + ]; + const isValid = allowedKeys.includes(key) || !isNaN(key as any); + if (!isValid) { + e.preventDefault(); + } + } } diff --git a/frontend/app/components/profile/template.hbs b/frontend/app/components/profile/template.hbs index bd4d84bd2..b55bcb880 100644 --- a/frontend/app/components/profile/template.hbs +++ b/frontend/app/components/profile/template.hbs @@ -50,6 +50,7 @@ minlength="4" maxlength="4" pattern="[0-9]{4}" + {{on "keydown" this.setBirthday}} {{on "change" (fn this.onInput "birthday")}} @warning={{this.warningErrorDate}} @model={{this.user}} diff --git a/frontend/app/components/registration-form/component.ts b/frontend/app/components/registration-form/component.ts index 7d9d145e3..86878eb57 100644 --- a/frontend/app/components/registration-form/component.ts +++ b/frontend/app/components/registration-form/component.ts @@ -151,4 +151,22 @@ export default class RegistrationFormComponent extends LoginFormComponent { setAgreedStatus(e: Document & any) { this.agreed = e.target.checked; } + + @action + setBirthday(e: Event) { + const key = e.key; + const allowedKeys = [ + 'Backspace', + 'Delete', + 'Tab', + 'Enter', + 'ArrowLeft', + 'ArrowRight', + ]; + const isValid = allowedKeys.includes(key) || !isNaN(key as any); + if (!isValid) { + e.preventDefault(); + } + + } } diff --git a/frontend/app/components/registration-form/template.hbs b/frontend/app/components/registration-form/template.hbs index ea2d5a953..ae93973d8 100644 --- a/frontend/app/components/registration-form/template.hbs +++ b/frontend/app/components/registration-form/template.hbs @@ -42,6 +42,7 @@ @placeholder={{t "registration_form.birthday_placeholder"}} @model={{this}} @name="birthday" + {{on "keydown" this.setBirthday}} />