notEmpty validator

Check if the value an is empty string

Options

Using with form field
The HTML attributes are used to set the validator options via the Declarative plugin
NameHTML attributeTypeDescription
messagedata-fv-not-empty___messageStringThe error message
trimdata-fv-not-empty___trimBooleanIf 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
// You might need to change the importing path
import notEmpty from 'formvalidation/dist/es6/validators/notEmpty';
const result = notEmpty().validate({
value: ...,
options: {
message: ...,
},
});
/*
result is an object of
{
valid: true or false,
message: The error message
}
*/
The MandatoryIcon plugin is useful when you want to display the mandatory icon for required fields

Basic example

In the following form, user is asked to enter the full name.
Basic example

HTML5 example

When the Declarative plugin is used, the notEmpty validator will be turned on automatically if the input uses HTML 5 required attribute.
HTML5 example

ES6 Module Example

The following snippet shows how to use the notEmpty validator with ES6 module:
// You might need to change the importing path
import notEmpty from 'formvalidation/dist/es6/validators/notEmpty';
const res1 = notEmpty().validate({
value: 'John Smith',
options: {
message: 'The name is required',
},
});
// res1.valid === true
const res2 = notEmpty().validate({
value: '',
options: {
message: 'The name is required',
},
});
// res2.valid === false
const res3 = notEmpty().validate({
value: ' ',
options: {
message: 'The name is required',
},
});
// res3.valid === true

See also

Changelog

v1.7.0
  • Provide new trim option