How to enter only letters and numbers in TextInput?
How to enter only letters and numbers? Something like the regular expression [a-zA-Z0-9_]
TextInput::make('telegram')
->label(__('page.profile.fields.telegram'))
->prefix('@')
->minLength(5)
->maxLength(32),
8 Replies
Help please 😔
->extraInputAttributes([‘pattern’ => ‘regrex’])
https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/pattern
MDN Web Docs
HTML attribute: pattern - HTML: HyperText Markup Language | MDN
The pattern attribute specifies a regular expression the form control's value should match. If a non-null value doesn't conform to the constraints set by the pattern value, the ValidityState object's read-only patternMismatch property will be true.
Or possibly use a mask()
Thank you.
I tried to do it through a mask, but I never found instructions on how to make only numbers and letters. Of course, I would like to do it through a mask. But how?
Probably better to do it with the pattern. Just let the browser handle it.
If you do ->extraInputAttributes([‘pattern’ => ‘regrex’]), then I think the backend validation will not work. If you remove this attribute in the browser through code viewing tools, the server will accept incorrect data.
you can't do that with ->mask, it's using alpine.js mask (https://alpinejs.dev/plugins/mask#x-mask) and it's handling Any, Only Alpha, Only Numeric.
But, you cant use this one https://filamentphp.com/docs/3.x/forms/validation#alpha-numeric
Mask — Alpine.js
Automatically format text fields as users type
This
Field::make('name')->alphaDash()
looks like what I need. Thank you. I'll try it tomorrow.