diff --git a/README.md b/README.md index 791b4ff..9dcb81b 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,6 @@ -# Overview - -This class easily adds front-end validation to HTML forms. - # Demo -A demo of the form validation class can be found [here](https://demos.danremollino.dev/form-validation/). +A feature demo can be found [here](https://demos.danremollino.dev/form-validation/). # Documentation @@ -20,10 +16,10 @@ 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 are first tested for blank. If this check is passed, fields +are then tested based on their `type` attribute value if applicable. (Described +[below](#markup).) Lastly, the field will test for a user supplied +[pattern](#pattern) if present. ## Required Fields @@ -450,9 +446,6 @@ copied and added to the field as a native [`pattern`](https://developer.mozilla. attribute. It is recommended to also provide an error message to display when the value does not match `pattern` by providing a [`patternMessage`](#patternmessage). -> The pattern is checked when input is present. A required field will always -first test for blank, then for the provided pattern. - ### patternMessage **Default:** `null` diff --git a/index.html b/index.html index 8642d61..4d2590f 100644 --- a/index.html +++ b/index.html @@ -106,7 +106,7 @@ Custom Form Controls
- +
@@ -115,11 +115,12 @@
- -
- - -
This custom form control checks if a the div#exists contains an image.
+ +
+ + +
This custom form control checks if a the div#exists contains an img tag.
@@ -128,21 +129,21 @@

These options can be applied to the fields above.

- + -
Exclude fields.
+
Optional fields will allow a blank input.
- + -
Exclude fields.
+
Optional fields will still be validated when there is input present.
(Numbers only.) -
This field uses a user supplied regular expression to test against and can output a custom error message.
+
This field uses a user supplied regular expression to test against and can output a custom error message. The value of the field will be run against all default validation checks, then run against the pattern.
diff --git a/validation.js b/validation.js index 45a7883..965fbf4 100644 --- a/validation.js +++ b/validation.js @@ -263,30 +263,28 @@ class Validation { parseField(fieldInstance) { const nameAttrVal = fieldInstance.el.name - this.validateForBlank(fieldInstance) + if (this.validateForBlank(fieldInstance)) { + switch (fieldInstance.el.type) { + case 'checkbox': + this.validateCheckbox(fieldInstance, nameAttrVal) + break + case 'email': + this.validateEmail(fieldInstance) + break + case 'password': + if (fieldInstance.el.name === 'password') this.validatePassword(fieldInstance) + if (fieldInstance.el.name === 'password-confirm') this.validateConfirmPass(fieldInstance) + break + case 'radio': + this.validateRadio(fieldInstance, nameAttrVal) + break + case 'tel': + this.validateTel(fieldInstance) + break + } - if (fieldInstance.el.value) { - if (fieldInstance.pattern) { + if (fieldInstance.pattern && fieldInstance.isValid) { this.validatePattern(fieldInstance) - } else { - switch (fieldInstance.el.type) { - case 'checkbox': - this.validateCheckbox(fieldInstance, nameAttrVal) - break - case 'email': - this.validateEmail(fieldInstance) - break - case 'password': - if (fieldInstance.el.name === 'password') this.validatePassword(fieldInstance) - if (fieldInstance.el.name === 'password-confirm') this.validateConfirmPass(fieldInstance) - break - case 'radio': - this.validateRadio(fieldInstance, nameAttrVal) - break - case 'tel': - this.validateTel(fieldInstance) - break - } } } } @@ -297,7 +295,9 @@ class Validation { fieldInstance, (fieldInstance.labelText ? fieldInstance.labelText : `Field`) + ` is required.` ) + return false } + return true } validatePattern(fieldInstance) {