Form Validation using JavaScript Functions
Validating a form with JavaScript - JavaScript - SitePoint Forums | Web Development & Design Community
Form validation on client side ?
MDN has a great one pager on this, lots of worthwhile stuff to know regarding some useful specs around it.
https://developer.mozilla.org/en-US/docs/Learn/HTML/Forms/Form_validation
Everyone ends up writing their own validation for some reason, I'd recommend leveraging the specs and existing libraries to save you the headache, unless it's to learn.
More on reddit.comJavaScript Form validation tutorial For beginners (** Link in 1st Comment)
It looks good overall, but you are missing a few accessibility measures:
-
The inputs should have the
requiredattribute. Plus if you are using your own inline errors, you should putnovalidateon the form. -
There should be dynamic focus management on the first invalid input.
-
The inline error should be connected to the input with
aria-describedby. -
Bonus points if you use
role="alert"andaria-live="assertive"to immediately notify the screen reader user. -
This is opinionated: the inline error should be removed on
changeandfocusout. That will match the UI of default HTML5 form validation.
How many types of form validation are there in JavaScript?
What are the benefits of using JavaScript for form validation?
Why JavaScript form data validation is not secure?
Videos
» npm install yup