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 with ES6 module
import notEmpty from 'formvalidation/dist/es6/validators/notEmpty';
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.
ES6 Module Example
The following snippet shows how to use the notEmpty validator with ES6 module:
import notEmpty from 'formvalidation/dist/es6/validators/notEmpty';
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