check-email-js
v1.0.2
Published
An NPM module that validates email addresses with an optional domain restriction.
Downloads
20
Maintainers
Readme
check-email-js
A simple and customizable module that validates email addresses with an optional domain restriction.
Installation
Using npm: $ npm install check-email-js
Using yarn: $ yarn add check-email-js
Usage
Importing
import { checkEmail } from 'check-email-js';
Function Signature
function checkEmail(email: string, options?: CheckEmailOptions): ValidationResult;
Options
The function accepts an optional object with the following options: | Property | Type | Default | Description | | -------- | ------------------ | ------- | ----------------------------------------------------------------------------------------------------------------------------------- | | domains | string or string[] | null | Domain restrictions for email validation. | | max | number | 3 | An optional number representing the maximum length of the top-level domain (TLD). If not provided, the default maximum length is 3. |
Return
The function returns an object with the following properties: | Property | Type | Description | | -------- | -------------- | ---------------------------------------------------------------------------------------------------------------- | | valid | boolean | Indicates if email is valid or not. | | error | string or null | Error message for invalid email or domain. Returns null if email is valid or if error message is not applicable. |
Example Usage
// Standard validation
const result = checkEmail('[email protected]');
console.log(result.valid); // Output: true
// With domain restriction
const result = checkEmail('[email protected]', { domains: 'example.com' });
console.log(result.valid); // Output: true
// With multiple domain restrictions
const result = checkEmail('[email protected]', { domains: ['sample.com', 'example.com', 'test.com'] });
console.log(result.valid); // Output: true
// With custom domain extension max length
const result = checkEmail('[email protected]', { max: 4 });
console.log(result.valid); // Output: true
License
This package is open-sourced software licensed under the MIT license.