-
I have a working text input with alpine using <!-- Accept only numbers and letter k -->
<input
x-data
x-mask="99.999.999-*"
x-on:keydown="return ($event.which >= 48 && $event.which <= 57) || $event.which == 75"
type="text"
{{ $attributes }}
/> I've read on mdn that I should ditch the which property in favor of key but I don't think that's it. The warning I get is
If I write So I guess my question is how can I write that keydown condition in a correct way using alpinejs? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 8 replies
-
What are you trying to achieve with return a value (yeah, you can't do it as it won't make sense). If you want to invalidate the keydown, use |
Beta Was this translation helpful? Give feedback.
-
the expression is already returned. That's how it works. So this expression becomes So there shouldn't be any different behaviors. Can you clarify what you really want to happen? |
Beta Was this translation helpful? Give feedback.
Chrome 126.0.6478.183 on Mac Sonoma (M2 chipset). I can type any letters.
As far as I know, returning false on keydown (standard event) is not part of any specs so I wouldn't rely on it (Although it used to work everywhere, not sure if it's still the case). What you really want is a dynamic mask