Skip to content

Commit

Permalink
fix: added implementation to block user from typing letters in input …
Browse files Browse the repository at this point in the history
…type tel
  • Loading branch information
Atharva Joshi authored and Atharva Joshi committed Apr 21, 2024
1 parent 9aaaf42 commit 94e9baf
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
18 changes: 18 additions & 0 deletions apps/www/registry/default/ui/input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,20 @@ export interface InputProps

const Input = React.forwardRef<HTMLInputElement, InputProps>(
({ className, type, ...props }, ref) => {
const validateInput = (event: React.KeyboardEvent<HTMLInputElement>) => {
const isKeycodeInRange =
event.keyCode >= 65 &&
event.keyCode <= 90 &&
!event.metaKey &&
!event.shiftKey &&
!event.altKey &&
!event.ctrlKey
const regex = /[a-zA-Z]/i
isKeycodeInRange &&
type === "tel" &&
regex.test(event.key) &&
event.preventDefault()
}
return (
<input
type={type}
Expand All @@ -16,6 +30,10 @@ const Input = React.forwardRef<HTMLInputElement, InputProps>(
)}
ref={ref}
{...props}
onKeyDown={(e) => {
validateInput(e)
props.onKeyDown?.(e)
}}
/>
)
}
Expand Down
18 changes: 18 additions & 0 deletions apps/www/registry/new-york/ui/input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,20 @@ export interface InputProps

const Input = React.forwardRef<HTMLInputElement, InputProps>(
({ className, type, ...props }, ref) => {
const validateInput = (event: React.KeyboardEvent<HTMLInputElement>) => {
const isKeycodeInRange =
event.keyCode >= 65 &&
event.keyCode <= 90 &&
!event.metaKey &&
!event.shiftKey &&
!event.altKey &&
!event.ctrlKey
const regex = /[a-zA-Z]/i
isKeycodeInRange &&
type === "tel" &&
regex.test(event.key) &&
event.preventDefault()
}
return (
<input
type={type}
Expand All @@ -16,6 +30,10 @@ const Input = React.forwardRef<HTMLInputElement, InputProps>(
)}
ref={ref}
{...props}
onKeyDown={(e) => {
validateInput(e)
props.onKeyDown?.(e)
}}
/>
)
}
Expand Down

0 comments on commit 94e9baf

Please sign in to comment.