0
# Core Validators
1
2
Essential validation functions for common form validation scenarios including required fields, string/numeric format validation, and length constraints.
3
4
## Capabilities
5
6
### Required Field Validation
7
8
Validates that a field is not empty, null, or undefined.
9
10
```javascript { .api }
11
/**
12
* Validates that a field has a value (not empty, null, or undefined)
13
* @returns ValidationRuleWithoutParams that returns true if field has value
14
*/
15
const required: ValidationRuleWithoutParams;
16
```
17
18
**Usage Examples:**
19
20
```javascript
21
import { required } from "@vuelidate/validators";
22
23
// The validators are validation objects with $validator, $message, and $params
24
const requiredValidator = required;
25
26
// In validation rules (most common usage)
27
const validationRules = {
28
username: { required },
29
email: { required }
30
};
31
32
// Direct validator usage (testing the internal validator function)
33
const isValid = requiredValidator.$validator("hello"); // true
34
const isEmpty = requiredValidator.$validator(""); // false
35
const isNull = requiredValidator.$validator(null); // false
36
```
37
38
### String Format Validators
39
40
Validators for checking string content patterns and character types.
41
42
#### Alpha Validator
43
44
Validates that a string contains only alphabetic characters.
45
46
```javascript { .api }
47
/**
48
* Validates that string contains only alphabetic characters (a-z, A-Z)
49
* @returns ValidationRuleWithoutParams that returns true if string is alphabetic
50
*/
51
const alpha: ValidationRuleWithoutParams;
52
```
53
54
#### Alphanumeric Validator
55
56
Validates that a string contains only alphanumeric characters.
57
58
```javascript { .api }
59
/**
60
* Validates that string contains only alphanumeric characters (a-z, A-Z, 0-9)
61
* @returns ValidationRuleWithoutParams that returns true if string is alphanumeric
62
*/
63
const alphaNum: ValidationRuleWithoutParams;
64
```
65
66
#### Numeric Validator
67
68
Validates that a string contains only numeric characters.
69
70
```javascript { .api }
71
/**
72
* Validates that string contains only numeric characters (0-9)
73
* @returns ValidationRuleWithoutParams that returns true if string is numeric
74
*/
75
const numeric: ValidationRuleWithoutParams;
76
```
77
78
**Usage Examples:**
79
80
```javascript
81
import { alpha, alphaNum, numeric } from "@vuelidate/validators";
82
83
// In validation rules (most common usage)
84
const validationRules = {
85
firstName: { alpha },
86
username: { alphaNum },
87
zipCode: { numeric }
88
};
89
90
// Direct validator function usage
91
const alphaValidator = alpha;
92
const isAlpha = alphaValidator.$validator("Hello"); // true
93
const notAlpha = alphaValidator.$validator("Hello123"); // false
94
95
const alphaNumValidator = alphaNum;
96
const isAlphaNum = alphaNumValidator.$validator("Hello123"); // true
97
const notAlphaNum = alphaNumValidator.$validator("Hello-123"); // false
98
99
const numericValidator = numeric;
100
const isNumeric = numericValidator.$validator("12345"); // true
101
const notNumeric = numericValidator.$validator("123abc"); // false
102
```
103
104
### Number Format Validators
105
106
Validators for specific numeric formats and patterns.
107
108
#### Decimal Validator
109
110
Validates that a value is a valid decimal number.
111
112
```javascript { .api }
113
/**
114
* Validates that value is a valid decimal number
115
* @returns ValidationRuleWithoutParams that returns true if value is decimal
116
*/
117
const decimal: ValidationRuleWithoutParams;
118
```
119
120
#### Integer Validator
121
122
Validates that a value is a valid integer.
123
124
```javascript { .api }
125
/**
126
* Validates that value is a valid integer
127
* @returns ValidationRuleWithoutParams that returns true if value is integer
128
*/
129
const integer: ValidationRuleWithoutParams;
130
```
131
132
**Usage Examples:**
133
134
```javascript
135
import { decimal, integer } from "@vuelidate/validators";
136
137
// In validation rules (most common usage)
138
const validationRules = {
139
price: { decimal },
140
quantity: { integer }
141
};
142
143
// Direct validator function usage
144
const decimalValidator = decimal;
145
const isDecimal = decimalValidator.$validator("123.45"); // true
146
const notDecimal = decimalValidator.$validator("abc"); // false
147
148
const integerValidator = integer;
149
const isInteger = integerValidator.$validator("123"); // true
150
const notInteger = integerValidator.$validator("123.45"); // false
151
```
152
153
### Length Constraint Validators
154
155
Validators for enforcing minimum and maximum length requirements on strings, arrays, and objects.
156
157
#### Minimum Length Validator
158
159
Validates that a value meets a minimum length requirement.
160
161
```javascript { .api }
162
/**
163
* Validates that value meets minimum length requirement
164
* @param min - Minimum length (number or Vue ref)
165
* @returns ValidationRuleWithParams with min parameter
166
*/
167
function minLength(min: number | Ref<number>): ValidationRuleWithParams<{ min: number }>;
168
```
169
170
#### Maximum Length Validator
171
172
Validates that a value does not exceed a maximum length.
173
174
```javascript { .api }
175
/**
176
* Validates that value does not exceed maximum length
177
* @param max - Maximum length (number or Vue ref)
178
* @returns ValidationRuleWithParams with max parameter
179
*/
180
function maxLength(max: number | Ref<number>): ValidationRuleWithParams<{ max: number }>;
181
```
182
183
**Usage Examples:**
184
185
```javascript
186
import { minLength, maxLength } from "@vuelidate/validators";
187
import { ref } from "vue";
188
189
// In validation rules (most common usage)
190
const validationRules = {
191
username: { minLength: minLength(3) },
192
title: { maxLength: maxLength(100) }
193
};
194
195
// Direct validator function usage for testing
196
const minLengthValidator = minLength(3);
197
const hasMinLength = minLengthValidator.$validator("hello"); // true (5 >= 3)
198
const tooShort = minLengthValidator.$validator("hi"); // false (2 < 3)
199
200
const maxLengthValidator = maxLength(10);
201
const withinMaxLength = maxLengthValidator.$validator("hello"); // true (5 <= 10)
202
const tooLong = maxLengthValidator.$validator("very long text"); // false (14 > 10)
203
204
// Reactive length validation with Vue refs
205
const minRef = ref(5);
206
const dynamicMinLength = minLength(minRef);
207
208
// Array length validation
209
const arrayLengthValidator = minLength(2);
210
const arrayValid = arrayLengthValidator.$validator(["item1", "item2"]); // true
211
```
212
213
### Value Constraint Validators
214
215
Validators for enforcing minimum and maximum value requirements on numbers.
216
217
#### Minimum Value Validator
218
219
Validates that a numeric value meets a minimum requirement.
220
221
```javascript { .api }
222
/**
223
* Validates that numeric value meets minimum requirement
224
* @param min - Minimum value (number, string, or Vue ref)
225
* @returns ValidationRuleWithParams with min parameter
226
*/
227
function minValue(min: number | Ref<number> | string | Ref<string>): ValidationRuleWithParams<{ min: number }>;
228
```
229
230
#### Maximum Value Validator
231
232
Validates that a numeric value does not exceed a maximum.
233
234
```javascript { .api }
235
/**
236
* Validates that numeric value does not exceed maximum
237
* @param max - Maximum value (number, string, or Vue ref)
238
* @returns ValidationRuleWithParams with max parameter
239
*/
240
function maxValue(max: number | Ref<number> | string | Ref<string>): ValidationRuleWithParams<{ max: number }>;
241
```
242
243
#### Between Validator
244
245
Validates that a numeric value is within a specified range.
246
247
```javascript { .api }
248
/**
249
* Validates that numeric value is between min and max (inclusive)
250
* @param min - Minimum value (number or Vue ref)
251
* @param max - Maximum value (number or Vue ref)
252
* @returns ValidationRuleWithParams with min and max parameters
253
*/
254
function between(min: number | Ref<number>, max: number | Ref<number>): ValidationRuleWithParams<{ min: number, max: number }>;
255
```
256
257
**Usage Examples:**
258
259
```javascript
260
import { minValue, maxValue, between } from "@vuelidate/validators";
261
import { ref } from "vue";
262
263
// In validation rules (most common usage)
264
const validationRules = {
265
age: { minValue: minValue(18) },
266
score: { maxValue: maxValue(100) },
267
percentage: { between: between(0, 100) }
268
};
269
270
// Direct validator function usage for testing
271
const minValueValidator = minValue(18);
272
const aboveMin = minValueValidator.$validator(25); // true (25 >= 18)
273
const belowMin = minValueValidator.$validator(15); // false (15 < 18)
274
275
const maxValueValidator = maxValue(100);
276
const belowMax = maxValueValidator.$validator(75); // true (75 <= 100)
277
const aboveMax = maxValueValidator.$validator(150); // false (150 > 100)
278
279
const betweenValidator = between(18, 65);
280
const inRange = betweenValidator.$validator(30); // true (18 <= 30 <= 65)
281
const outOfRange = betweenValidator.$validator(70); // false (70 > 65)
282
283
// Reactive value validation
284
const minAge = ref(21);
285
const dynamicMinValue = minValue(minAge);
286
287
// String number validation
288
const stringMinValue = minValue("18");
289
const stringValue = stringMinValue.$validator("25"); // true (parses strings to numbers)
290
```