ean validator

Validate an EAN (International Article Number)

Options

Using with form field
The HTML attributes are used to set the validator options via the Declarative plugin
NameHTML attributeTypeDescription
messagedata-fv-ean___messageStringThe error message
Using the ES6 module
// You might need to change the importing path
import { ean } from '/vendors/@form-validation/cjs/validator-ean';
const result = ean().validate({
value: ...,
options: {
message: ...,
},
});
/*
result is an object of
{
valid: true or false,
message: The error message
}
*/
Using the npm package
  • Install the validator package:
$ npm install @form-validation/validator-ean
  • Use the ean validator:
import { ean } from '@form-validation/validator-ean';
const result = ean().validate({
value: ...,
options: {
message: ...,
},
});

Basic example

ean validator

NPM package example

The following snippet shows how to use the ean validator with the npm package:
import { ean } from '@form-validation/validator-ean';
const res1 = ean().validate({
value: '9780471117094',
options: {
message: 'The value is not valid EAN',
},
});
// res1.valid === true
const res2 = ean().validate({
value: '73513536',
options: {
message: 'The value is not valid EAN',
},
});
// res2.valid === false

See also

Changelog

v2.0.0
  • Add the npm package
v1.5.0
  • Support GTIN-14 format