A nice JS package to validate the existence of a brazilian phone number's area code (called DDD). ๐ง๐ท โ๏ธ
First things first - install the package using npm
or yarn
:
# using npm
npm i validate-ddd-br
# using yarn
yarn add validate-ddd-br
After that, import the helper wherever you want to use it:
// es-modules
import { validateDDD } from 'validate-ddd-br';
// commonjs
const validateBrazilianDDD = require('validate-ddd-br');
Then, you'll be able to use it:
// with integer value
const isValid = validateDDD(41); // true
// commonjs = const isValid = validateBrazilianDDD.validateDDD(41);
// with string value
const isValid = validateDDD('41'); // true
// commonjs = const isValid = validateBrazilianDDD.validateDDD('40');
// or with a phone number - it must be provided within a mask to work!
const isValid = validateDDD('(12) 99999-9999'); // true
// commonjs = const isValid = validateBrazilianDDD.validateDDD('(12) 99999-9999');
It can also tell from where in Brazil the DDD is:
import { isFrom } from 'validate-ddd-br';
const whereFrom = isFrom('21'); // { region: 'Rio de Janeiro', state: 'RJ' }
// commonjs = const isValid = validateBrazilianDDD.isFrom('21');
const whereFrom = isFrom('(31) 99999-9999'); // { region: 'Belo Horizonte', state: 'MG' }
// commonjs = const isValid = validateBrazilianDDD.isFrom('(31) 99999-9999');
Important: both methods accept phone numbers, but you must provide them within the following mask: (xx) xxxxx-xxxx
First, fork the project. After it, install the dependencies (preferably using npm - since the project is using it) and do the work.
Also, take a look at the contributing guide!
Licensed under the MIT.