Responsive age calculator app with input validation
Design comparison
Solution retrospective
Hello fellow Frontend devs!
It's been a while since my last challenge. So here is my latest work. Built with vanilla CSS, JavaScript and Vite.
For this particular project, the main obstacle I did encountered is creating the input validation. I didn't really had the idea on how to validate both the input along with checking whether the date entered by user is valid to be calculated. So what did I wrote for the validations?
Well firstly the application will check whether the user has entered all the required fields. If they failed to do so, it will show a required field error message.
if (inputValue.length === 0) { errorText.innerHTML = "This field is required"; }
Next is the date validation. On the date validation, I wrote a function that return the error status of each of the date input as an object.
function dateIsValid(..params){
let isError = {
dayHasError: false,
monthHasError: false,
yearHasError: false,
};
// validating...
return { isError }
}
So on the main application file, I can just called the returned value like this:
const { isError } = dateIsValid(..params);
To access the value of each date input: isError.dayHasError | isError.monthHasError | isError.yearHasError
Check out the project repository here for the full code.
Feedbacks are welcome :D
Community feedback
- @ngugimuchangiPosted over 1 year ago
Hey Zubair.
Great work on the application.
I have a few suggestions that might help you with validation:
- Check out the JustValidate library, it helps work on input validation in a much easier and more organized way.
- Also HTML offers pattern matching and other input attributes such as min, max, and required. You can use
checkValidity()
instance method of HTML input elements to check if the input fields, and CSS pseudoselectors such as:invalid
and:valid
to adjust styling for input fields based on validity.
JustValidate is an easier route. I hope this helps. Cheers!
Marked as helpful1@atmahanaPosted over 1 year ago@ngugimuchangi Hey Duncan, thank you for the great suggestions.
I did insisted myself from using any library for this particular challenge because I want to challenge my JavaScript skill.
And as per your suggestions, it is indeed better to use library for more organized code. Hence, I will make sure to look into the library in improving my form validation and overall code.
Cheers!
0
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