notEmpty validator
Check if the value an is empty string
Options
Using with form field
Name | HTML attribute | Type | Description |
---|
message | data-fv-not-empty___message | String | The error message |
trim | data-fv-not-empty___trim | Boolean | If true , all spaces at the beginning and the end of field value will be removed before being validated. It is false by default |
Use with select element
If you want a select element to be required, you have to set value=""
for the option which is treated as empty one:
<select name="gender">
<option value="">Select the gender</option>
<option value="male">Male</option>
<option value="female">Female</option>
<option value="other">Other</option>
</select>
Using the ES6 module
import { notEmpty } from '/vendors/@form-validation/cjs/validator-not-empty';
const result = notEmpty().validate({
value: ...,
options: {
message: ...,
},
});
Using the npm package
- Install the validator package:
$ npm install @form-validation/validator-not-empty
- Use the
notEmpty
validator:
import { notEmpty } from '@form-validation/validator-not-empty';
const result = notEmpty().validate({
value: ...,
options: {
message: ...,
},
});
Basic example
In the following form, user is asked to enter the full name.
HTML5 example
When the
Declarative plugin is used, the notEmpty validator will be turned on automatically if the input uses HTML 5
required
attribute.
NPM package example
The following snippet shows how to use the notEmpty validator with the npm package:
import { notEmpty } from '@form-validation/validator-not-empty';
const res1 = notEmpty().validate({
value: 'John Smith',
options: {
message: 'The name is required',
},
});
const res2 = notEmpty().validate({
value: '',
options: {
message: 'The name is required',
},
});
const res3 = notEmpty().validate({
value: ' ',
options: {
message: 'The name is required',
},
});
See also
Changelog