allow only number in phone number input (#10)
All checks were successful
delpoy / build-and-deploy (push) Successful in 41s

This commit is contained in:
2023-11-30 20:20:12 +01:00
parent 4e16487d3d
commit 971ca4bc75
2 changed files with 26 additions and 6 deletions

View File

@@ -7,6 +7,7 @@
export let type = 'text';
export let value: string | null = null;
export let placeholder: string | null = null;
export let pattern: RegExp | null = null;
export let required = false;
export let disabled = false;
export let readonly = false;
@@ -91,10 +92,22 @@
bind:this={inputElement}
autocomplete="off"
on:input={(e) => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
value = e.target?.value;
input(e);
if (pattern && !pattern.test(e.target?.value)) {
if (inputElement?.value.endsWith(e.data)) {
value = e.target?.value.substring(0, -e.data.length);
}
return false;
}
return input(e);
}}
on:paste={(e) => {
if (
pattern &&
!pattern.test((e.clipboardData || window.clipboardData).getData('text'))
) {
e.preventDefault();
}
}}
on:click={click}
/>