Design comparison
Solution retrospective
I'm not happy with the result at all. While all the fields can be filled out, the validations are not working as expected. Initially, the form cannot be submitted if the fields are empty, but the error messages don't disappear when they're filled. Additionally, I couldn't implement a validation for the expiration date. I could use a similar approach as the card number validation, but that would mean repeating the previous validation. If someone could take a look at my solution, I would appreciate it.
Community feedback
- @JAleXDesigNPosted over 1 year ago
Hello, so that the errors disappear when the fields are full, you can perform a validation with the onblur event, if the field meets the validation requirements, you remove the error from said field. To validate the expiration date: Here is an example of how you can do it:
inputExpMonth.onblur = () => { //Since you are using an input type text you must convert the value to a number, if you use inputs type number this is not necessary const month = Number(inputExpMonth.value); //You check if it is a valid number with !isNaN() and also that it is between 1 and 12 const isValidMonth = !isNaN(month) && month > 0 && month <= 12; if (!isValidMonth) { //if it is not valid } } inputExpYear.onblur = () => { const year = Number(inputExpYear.value); //Get the last two digits of the current year const actualYear = new Date().getFullYear() % 100; //check if it is a valid number and that it is greater than or equal to the current year const isValidYear = !isNaN(year) && year >= actualYear; if (!isValidMonth) { //if it is not valid } }
Marked as helpful0
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