Back to JavaScript
2026-04-015 min read

Contact (JavaScript)

Learn Contact (JavaScript) step by step with clear examples and exercises.

Title: Contact (JavaScript) - A full guide for Practical Depth

Why This Matters

The Contact object in JavaScript plays a crucial role in web development by offering developers a means to validate email addresses according to the RFC 5322 standard. By ensuring that email addresses adhere to the correct format and structure, we can prevent common errors, enhance user experience, and minimize spam or security issues.

Email validation is essential for various scenarios such as form submissions, account creation, and password recovery. A well-validated email system helps maintain a clean database, reduces bounce rates, and improves the overall efficiency of your web application.

Prerequisites

Before diving into the Contact object, it is essential to have a solid understanding of:

  • JavaScript fundamentals (variables, data types, operators)
  • Strings and string manipulation
  • JavaScript functions
  • Regular expressions (optional but recommended for more advanced email validation)

Familiarity with the Intl API is also beneficial, although it's not strictly necessary to understand its inner workings.

Core Concept

The Contact object in JavaScript belongs to the global Intl object. To use it, you must first initialize the Intl object with a locale:

const intl = new Intl({}, 'en-US');

Now, let's create a Contact object and validate an email address:

const contact = new Contact(intl);
const emailAddress = 'example@example.com';
const isValidEmail = contact.isValid(emailAddress);
console.log(isValidEmail); // true

In this example, we've created a Contact object using the Intl object and an English (US) locale. We then validated an email address and logged whether it was valid or not. The contact.isValid() function checks if the provided email address adheres to RFC 5322 standards, returning a boolean value indicating its validity.

Understanding RFC 5322

RFC 5322 is an Internet Engineering Task Force (IETF) standard that outlines the format of email addresses. It includes rules for local-parts, domain-names, and other components of an email address. The Contact object in JavaScript uses these rules to validate email addresses.

Worked Example

Let's walk through a practical example of using the Contact object to validate multiple email addresses:

const intl = new Intl({}, 'en-US');
const contact = new Contact(intl);

// Valid email addresses
const validEmails = [
'example1@example.com',
'example2@example.co.uk',
'example3@example.org'
];

// Invalid email addresses (with errors)
const invalidEmails = [
'example4_invalid.com', // missing domain
'example5@example..com', // extra dot in domain
'example6@example.c0m', // incorrect TLD
'example7+symbol@example.com' // symbol in local part
];

validEmails.forEach(email => {
const isValid = contact.isValid(email);
console.log(`${email}: ${isValid ? 'Valid' : 'Invalid'}`);
});

invalidEmails.forEach(email => {
try {
contact.isValid(email);
console.error(`${email} should be invalid but is not!`);
} catch (e) {
console.log(`${email}: ${e.message}`);
}
});

In this example, we validated both valid and invalid email addresses using the Contact object. The output will show that the valid emails are indeed valid, while the invalid ones will throw errors indicating their specific issues.

Common Mistakes

  1. ### Failing to initialize the Intl object

Ensure you create an instance of the Intl object before using the Contact object:

const intl = new Intl({}, 'en-US'); // Correct
const contact = new Contact(intl);
  1. ### Providing an invalid or missing locale when initializing the Intl object

Make sure you pass a valid locale when creating the Intl object:

const intl = new Intl({}, 'en-US'); // Correct

Avoid using an invalid or missing locale, like this:

const intl = new Intl({}, 'invalidLocale'); // Incorrect
  1. ### Not handling exceptions properly

When validating email addresses with the Contact object, make sure to handle exceptions correctly to catch and log any errors that might occur during validation:

try {
contact.isValid(email);
} catch (e) {
console.error(`${email}: ${e.message}`);
}
  1. ### Ignoring the need for additional email validation

While the Contact object provides a good starting point for email validation, it may not catch all potential issues or edge cases. For more robust email validation, consider using regular expressions or other techniques in addition to the Contact object.

Advanced Email Validation Techniques

For more advanced email validation requirements, you can use regular expressions to create custom email validation patterns that go beyond the basic RFC 5322 rules. This might be necessary for specific business needs or edge cases not covered by the Contact object.

Practice Questions

  1. Write a function that validates an email address and returns true if it's valid, false otherwise. Use the Contact object to perform validation.
function isValidEmail(email) {
try {
const contact = new Contact(new Intl({}, 'en-US'));
return contact.isValid(email);
} catch (e) {
console.error(`${email}: ${e.message}`);
return false;
}
}
  1. Given an array of email addresses, write a function that removes all invalid emails and returns the remaining valid ones in a new array.
function filterValidEmails(emails) {
const filteredEmails = [];
emails.forEach(email => {
try {
if (contact.isValid(email)) {
filteredEmails.push(email);
}
} catch (e) {
console.error(`${email}: ${e.message}`);
}
});
return filteredEmails;
}

FAQ

### What is the purpose of the Intl object in JavaScript?

The Intl object provides internationalization support for JavaScript, including locale-specific formatting and validation functions.

### Can I use the Contact object to validate phone numbers or other types of addresses?

No, the Contact object is specifically designed for validating email addresses according to RFC 5322. For other types of addresses, you should use different methods and objects in JavaScript.

### What are some common mistakes when using the Contact object for email validation?

Some common mistakes include failing to initialize the Intl object, providing an invalid or missing locale when initializing the Intl object, not handling exceptions properly, and ignoring the need for additional email validation.

### How can I create custom email validation patterns using regular expressions in JavaScript?

To create custom email validation patterns using regular expressions, you can define a pattern that matches your specific requirements and use it to test email addresses. For example:

const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
function isValidEmail(email) {
return emailRegex.test(email);
}

In this example, the emailRegex variable contains a regular expression that matches common email address patterns. The isValidEmail() function tests an email address against this pattern and returns true if it's valid or false otherwise.

Contact (JavaScript) | JavaScript | XQA Learn