DEV Community

Susheel kumar
Susheel kumar

Posted on

Email Validation with Custom Rules Using saksh-email-validator

Introduction

Email validation is a crucial step in ensuring that the email addresses collected through forms or other means are valid and meet specific criteria. In this blog post, we'll explore how to use the saksh-email-validator npm package to validate email addresses with custom rules.

Setting Up

First, install the saksh-email-validator package by running the following command in your terminal:

npm install saksh-email-validator
Enter fullscreen mode Exit fullscreen mode

Importing the Validator

Next, we start by importing the sakshValidateEmail function from the package:

import { sakshValidateEmail } from 'saksh-email-validator';
Enter fullscreen mode Exit fullscreen mode

Custom Validation Rules

We will define two custom validation rules:

1. Disallow Emails Containing "test"

This rule checks if the email contains the word "test" and marks it as invalid if it does.

function customRule1(email) {
    return {
        valid: !email.includes('test'),
        reason: 'Emails containing "test" are not allowed.',
        priority: 1 // Higher priority
    };
}
Enter fullscreen mode Exit fullscreen mode

2. Disallow Emails from "example.com"

This rule checks if the email domain is "example.com" and marks it as invalid if it is.

function customRule2(email) {
    const domain = email.split('@')[1];
    return {
        valid: domain !== 'example.com',
        reason: 'Emails from "example.com" are not allowed.',
        priority: 2 // Lower priority
    };
}
Enter fullscreen mode Exit fullscreen mode

Configuration Object

We create a configuration object that includes our custom validation rules and other settings:

const config = {
    validateMx: true,
    validateSmtp: true,
    blacklistedDomains: [],
    disposableDomains: [],
    roleBasedEmails: [],
    wellKnownDomains: ['sakshamapp.com'],
    customValidationRules: [customRule1, customRule2]
};
Enter fullscreen mode Exit fullscreen mode

Example Email Addresses

We define a list of email addresses to test our validation:

const emails = [
    'valid.email@allowed.com',
    'test.email@allowed.com',
    'valid.email@example.com',
    'test.email@example.com',
    'susheelhbti@gmail.com',
    'saksh@sakshamapp.com',
    'susheel@aistore2030.com'
];
Enter fullscreen mode Exit fullscreen mode

Testing the Email Validation

We create an asynchronous function to test the email validation for each email address in our list:

async function testEmailValidation() {
    for (const email of emails) {
        const result = await sakshValidateEmail(email, config);
        console.log(`Validation result for ${email}:`, result);
    }
}

testEmailValidation();
Enter fullscreen mode Exit fullscreen mode

Clearing the Cache

If needed, we can clear the cache used by the validator:

function clearCache() {
    sakshCache.flushAll();
    console.log('Cache cleared.');
}

// Uncomment to clear the cache
// clearCache();
Enter fullscreen mode Exit fullscreen mode

Conclusion

By using the saksh-email-validator package, we can easily validate email addresses with custom rules and configurations. This ensures that the email addresses we collect are valid and meet our specific criteria, enhancing the quality of our data collection processes.

Feel free to use this explanation for your blog post. If you need any further adjustments or additional information, just let me know!

Top comments (0)