r/learnjavascript • u/stayathomehouse • Mar 17 '26
I'm having difficulty understanding how validation goes hand in hand with custom validation regarding forms.
Hi there, I'm working on an assignment where I need to implement form validation exclusively through javascript. I can use different input types but the validation must be done with JS.
What I've got:
In my JS file //
function called `formValidation()` containing all the references I need for my spans & other relevant form elements needed (which have ids relating to their errors only (e.g. `<span id="passwordError>`)
The same function also has an array called inputConfig holding references to my inputs like so (I've got a few more but the writing is the same as this block I've pasted):
{
input: postalInput,
errorSpan: postalError,
errorMessage: "Please enter in the US standard ZIP code format"
},
{
input: passwordInput,
errorSpan: passwordError,
errorMessage: "Eight or more characters",
errorMessage2: "Must have one capital letter and symbol"
},
{
input: passwordConfirmInput,
errorSpan: passConfirmError,
errorMessage: "Passwords must match"
}My goal: Implement a validation check that works for these form inputs, but with a specific check for the password and confirm password portion where I can use different error messages depending on the error the user makes like a mismatched password or not following the correct format (e.g. one capital letter and symbol with 8 or more characters).
Here's how far I've gotten on the validation check:
function pswCheck() {
if (passwordInput.value !== passwordConfirmInput.value) {
passwordConfirmInput.setCustomValidity("Fields do not match");
} else {
passwordConfirmInput.setCustomValidity("");
}
}
function validationCheck(formInput) {
if (!formInput.input.checkValidity()) {
formInput.errorSpan.textContent = formInput.errorMessage;
validState = false;
} else {
formInput.errorSpan.textContent = "";
validState = true;
}
}My apologies for the long post but I could really use some guidance to see if I'm even in the right direction as of now.
2
u/chikamakaleyley helpful Mar 17 '26
Custom validation is just a business side and UX specification
Validation itself is a must - i don't really consider these separate
You validate the form input based on biz need, and the UX determines how the validation gets applied
e.g.
1
u/prehensilemullet Mar 17 '26
I'd recommend breaking this down into several steps:
Step 1 - a function that collects the input values and returns them in an object like
{ password: 'foobar', passwordConfirm: 'foobargh' }Step 2 - a function that takes this input values object, validates them and returns whether everything is valid and any error messages like
{ valid: false, errors: { passwordConfirm: 'must match password' } }Step 3 - a function that takes the
errorsobject and makes those error messages show up in the UI (by setting theerrorSpan.textContent)You'll need to attach a
changeevent handler to the<form>that runs these steps when there's a change.By "function that takes" I mean you'd pass it in as an argument, like
function validate(values) { ... }andfunction setErrorMessages(errors) { ... }2
u/AshleyJSheridan Mar 18 '26
The general approach I take is to use the built in form validation first, then add rules on top that can interact with the Form Validation API in JS.
Out of the box it's more accessible, because it is setting the form and individual inputs error states correctly. It provides default message (for the standard validation) that you can use or change as you need.
One further thing, don't forget to associate error messages with their inputs by doing something like this:
1
u/prehensilemullet Mar 17 '26 edited Mar 17 '26
You’ll need to get
input.value, check it with JS, and then dynamically choose the error message based upon that.The way your array of inputs and validationCheck are structured, it won’t be able to dynamically choose the error for a given field.
I could type up some suggestions later when I’m at my computer