@jdlien/validator 1.4.4 → 1.4.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +13 -13
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -17,7 +17,7 @@ Validator includes the following built-in validation types:
|
|
|
17
17
|
- Pattern (regular expression)
|
|
18
18
|
- Numbers (with decimals and negative values)
|
|
19
19
|
- Integers (positive whole numbers)
|
|
20
|
-
- North
|
|
20
|
+
- North American Phone Numbers
|
|
21
21
|
- US Zip Codes
|
|
22
22
|
- Email Addresses
|
|
23
23
|
- Canadian Postal Codes
|
|
@@ -26,7 +26,7 @@ Validator includes the following built-in validation types:
|
|
|
26
26
|
- Time of day
|
|
27
27
|
- URLs
|
|
28
28
|
|
|
29
|
-
You can also add custom validation
|
|
29
|
+
You can also add custom validation and customize error messages per field or for the whole form.
|
|
30
30
|
|
|
31
31
|
Validator is compatible with all modern browsers. It has no dependencies (other than its validator-utils package). It is written in TypeScript with 100% test coverage.
|
|
32
32
|
|
|
@@ -44,12 +44,12 @@ yarn add @jdlien/validator
|
|
|
44
44
|
|
|
45
45
|
Create a form as you normally would, adding attributes for inputs to control how Validator will
|
|
46
46
|
check the input, such as `required`, `type`, or `data-type`. Native HTML5 attributes are supported,
|
|
47
|
-
although often the browser's built-in validation is problematic or inflexible. In those cases, you
|
|
47
|
+
although often, the browser's built-in validation is problematic or inflexible. In those cases, you
|
|
48
48
|
can use a `data-` variant of these attributes to avoid the browser's built-in validation.
|
|
49
49
|
|
|
50
50
|
Any input that you want to validate should have a unique name attribute. If you want to display error messages for the input, you must also have a div with an id that is the name of the input + `-error`.
|
|
51
51
|
|
|
52
|
-
Then create a new Validator instance and pass it the form element as the first argument. An optional second argument allows you to pass in options. Here is a simplified example:
|
|
52
|
+
Then, create a new Validator instance and pass it the form element as the first argument. An optional second argument allows you to pass in options. Here is a simplified example:
|
|
53
53
|
|
|
54
54
|
```html
|
|
55
55
|
<form id="myForm">
|
|
@@ -90,7 +90,7 @@ When initialized, Validator disables browser validation and displays error messa
|
|
|
90
90
|
2. The ID of the input + `-error`
|
|
91
91
|
3. The name of the input + `-error`
|
|
92
92
|
|
|
93
|
-
Using the aria-describedby attribute to link an error message with an input is the most robust and effective method,
|
|
93
|
+
Using the aria-describedby attribute to link an error message with an input is the most robust and effective method. Also, this enhances accessibility by enabling screen readers to announce the error when the input is focused.
|
|
94
94
|
|
|
95
95
|
If you wish to customize the default error message, you can also set one for a field using `data-error-default`.
|
|
96
96
|
|
|
@@ -98,18 +98,18 @@ If you wish to customize the default error message, you can also set one for a f
|
|
|
98
98
|
|
|
99
99
|
[Working demo on jdlien.com](https://jdlien.com/validator/demo/).
|
|
100
100
|
|
|
101
|
-
## Supported Input Types and
|
|
101
|
+
## Supported Input Types and Attributes
|
|
102
102
|
|
|
103
103
|
Validator works by checking for certain attributes on the form inputs and applying validation based on those.
|
|
104
|
-
In many cases you can use the native HTML5 attributes, but you can also use the `data-` attributes if you do not want the behavior to be affected by built-in browser validation behavior (e.g., for min-length, max-length, and input types such as date and time).
|
|
104
|
+
In many cases, you can use the native HTML5 attributes, but you can also use the `data-` attributes if you do not want the behavior to be affected by built-in browser validation behavior (e.g., for min-length, max-length, and input types such as date and time).
|
|
105
105
|
|
|
106
106
|
There are a few attributes that Validator looks for on the form element:
|
|
107
107
|
|
|
108
|
-
- `data-prevent-submit` - If this attribute is present, the form will never be submitted, even if it is valid. This is useful if you want to handle the submission yourself. (By default the form will be submitted if it is valid and not if it is invalid.)
|
|
108
|
+
- `data-prevent-submit` - If this attribute is present, the form will never be submitted, even if it is valid. This is useful if you want to handle the submission yourself. (By default, the form will be submitted if it is valid and not if it is invalid.)
|
|
109
109
|
|
|
110
|
-
- `novalidate` - This is a native HTML5 attribute that will disable browser validation on the form. If this attribute is present. Validator adds this by default and
|
|
110
|
+
- `novalidate` - This is a native HTML5 attribute that will disable browser validation on the form. If this attribute is present. Validator adds this by default and removes it if `destroy()` is called. If you add it yourself, it will not be added back by Validator.
|
|
111
111
|
|
|
112
|
-
On input (and sometimes select and textarea) elements the following attributes are supported:
|
|
112
|
+
On input (and sometimes select and textarea) elements, the following attributes are supported:
|
|
113
113
|
|
|
114
114
|
- `required` - The input must have a value.
|
|
115
115
|
- `minlength`/`data-min-length` - The input must be at least the specified number of characters.
|
|
@@ -119,7 +119,7 @@ On input (and sometimes select and textarea) elements the following attributes a
|
|
|
119
119
|
|
|
120
120
|
- `number` - The input must be a number (use `data-type` to avoid quirky browser behavior)
|
|
121
121
|
- `integer` - The input must be a positive whole number.
|
|
122
|
-
- `tel` - The input must be a valid North
|
|
122
|
+
- `tel` - The input must be a valid North American phone number.
|
|
123
123
|
- `email` - The input must be a valid email address.
|
|
124
124
|
- `zip` - The input must be a valid US zip code.
|
|
125
125
|
- `postal` - The input must be a valid Canadian postal code.
|
|
@@ -135,7 +135,7 @@ On input (and sometimes select and textarea) elements the following attributes a
|
|
|
135
135
|
- `data-validation` - The name of a custom validation function.
|
|
136
136
|
- `data-novalidate` - If this attribute is present, the input will not be validated when `input` or `change` events are triggered on it.
|
|
137
137
|
|
|
138
|
-
A validation function will be called with the input value as the argument. The function may either return a boolean (true/false) or an object with a `valid` property that is a boolean. If the function returns string
|
|
138
|
+
A validation function will be called with the input value as the argument. The function may either return a boolean (true/false) or an object with a `valid` property that is a boolean. If the function returns a string or an object with a `message` property, that will be used as the error message for the input. A `messages` array may also be specified which will be used to display multiple error messages for the input.
|
|
139
139
|
|
|
140
140
|
You may also use a promise that resolves to such an object for asynchronous validation.
|
|
141
141
|
|
|
@@ -174,7 +174,7 @@ You can customize the class(es) that Validator uses to hide the error messages b
|
|
|
174
174
|
|
|
175
175
|
## Color Picker Support
|
|
176
176
|
|
|
177
|
-
If you need to allow a user to pick a color, you can use data-type="color" and the input will be required to be any valid CSS color supported by the browser. This type can also work in conjunction with a native color input. If you do this, you will need to add an input with `type="color"` and the name of the data-color input + `-color`. This should be inside a linked label which will become the color preview swatch. Such a label should have an ID of the color input's name + `-color-label` so that Validator can change the background to the specified color.
|
|
177
|
+
If you need to allow a user to pick a color, you can use data-type="color" and the input will be required to be any valid CSS color supported by the browser. This type can also work in conjunction with a native color input. If you do this, you will need to add an input with `type="color"` and the name of the data-color input + `-color`. This should be inside a linked label, which will become the color preview swatch. Such a label should have an ID of the color input's name + `-color-label` so that Validator can change the background to the specified color.
|
|
178
178
|
|
|
179
179
|
A basic example that would work:
|
|
180
180
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jdlien/validator",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.6",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"module": "dist/validator.js",
|
|
6
6
|
"types": "dist/Validator.d.ts",
|
|
@@ -58,6 +58,6 @@
|
|
|
58
58
|
},
|
|
59
59
|
"sideEffects": false,
|
|
60
60
|
"dependencies": {
|
|
61
|
-
"@jdlien/validator-utils": "^1.2.
|
|
61
|
+
"@jdlien/validator-utils": "^1.2.8"
|
|
62
62
|
}
|
|
63
63
|
}
|