Credit card component with css custom properties and vanilla js
Design comparison
Solution retrospective
Hi everyone! After completing a few newbie projects, I've put my hands on this one and run into some problems:
Ad 1. I can't put maximum length on type number inputs in html, I've tried to do that with js but failed miserably, any ideas?
Ad 2. How to restrict input fields eg. in MM field how do I restrict it to only take inputs of 1 to 12? When using html max attribute I only get a text message and can still enter any number I want.
Community feedback
- @ErayBarslanPosted about 2 years ago
Hey there, congrats on your solution!
- Unfortunatelly maxlength doesn't work as expected with number inputs. I prefer to use
type: text
and when I need to call the input to JS I convert it likeNumber(input)
. If you wish to keep it as number though in JS you can create a function:
const maxChar = (el, max) => { if (el.value.length > max) { el.value = el.value.slice(0, max) } } <input ... type="number" onkeypress="maxChar(this, 16)" />
Also as for
<label>
elements, name should match with the input id like:<label for="name">cardholder name</label> <input id="name" />
Other than these not much I'd change. Happy coding :)
Marked as helpful0@kamilp522Posted about 2 years agoHi, thanks for the feedback! :D I'll probably try to convert text into number by js which now seems so obvious, but hey, thats part of the learning process. Thanks again
0 - Unfortunatelly maxlength doesn't work as expected with number inputs. I prefer to use
Please log in to post a comment
Log in with GitHubJoin our Discord community
Join thousands of Frontend Mentor community members taking the challenges, sharing resources, helping each other, and chatting about all things front-end!
Join our Discord