From 3a3339f90fb8df3aa2b2a748cb04b284e77ea12a Mon Sep 17 00:00:00 2001 From: Dan Remollino Date: Wed, 2 Apr 2025 16:00:51 -0400 Subject: [PATCH] initial commit --- README.md | 463 ++++++++++++++++++++++++++++++++++++++++++++++++++ validation.js | 450 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 913 insertions(+) create mode 100644 README.md create mode 100644 validation.js diff --git a/README.md b/README.md new file mode 100644 index 0000000..165ee51 --- /dev/null +++ b/README.md @@ -0,0 +1,463 @@ +# Form Validation + +Importing the `Validation` class queries the current page for `
` elements +and disables the default browser [constraint validation](https://developer.mozilla.org/en-US/docs/Web/HTML/Constraint_validation) +by adding a `novalidate` attribute to that element. + +The default behavior is to validate each field when their value is updated, +and validate the entire form when it is submitted. This can be changed to only +validate on form submission by setting [`onlyOnSubmit`](#onlyonsubmit) to `true` on the +instance. + +Once a field is marked invalid, it will always re-validate when its value is +changed regardless of the [`onlyOnSubmit`](#onlyOnSubmit) value. + +> `required` fields are tested for blank first. If this check is passed, the +field will then test for [custom rules](#custom-validation) if present. If +there are no custom rules, some fields are also tested further based on their +`type` attribute value. These are described [below](#markup). + +## Required Fields + +Usually, the majority of fields in a form need validation. To make setup +easier, instances of the *Validation* class have a default [`reqAll`](#reqall) +value of `true`. This marks all fields that meet the following criteria +required and needing validation: + +* Have a `name` attribute. +* Not [`disabled`](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/disabled). + +Indivudual fields can then be excluded from validation as described [below](#reqall). + +This functionality can be inverted by setting [`reqAll`](#reqall) to `false`, +and manually giving your required fields a [`required`](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/required) +attribute. + +```html +
+ + +
+``` + +`` `type` attribute values of `checkbox` and `radio` only need one option to have the +`required` attribute, however, it is recommended to add it to all options for +consistency. More information on checkbox and radio fields can be found [below](#checkbox-and-radio-fields). + +### Required Indicator + +A `` tag with class of `required__indicator` containing an asterisk(*) +character will be inserted [`beforeend`](https://developer.mozilla.org/en-US/docs/Web/API/Element/insertAdjacentHTML#beforeend) +of a required field's `