How to use the normalizeEmail function from validator

Find comprehensive JavaScript validator.normalizeEmail code examples handpicked from public code repositorys.

validator.normalizeEmail is a function in the validator library that normalizes an email address by converting it to lowercase and removing extra whitespace.

26
27
28
29
30
31
32
33
34
35

if (validationErrors.length) {
  req.flash("errors", validationErrors);
  return res.redirect("/login");
}
req.body.email = validator.normalizeEmail(req.body.email, {
  gmail_remove_dots: false,
});

passport.authenticate("local", (err, user, info) => { // passport.authenticate() is a middleware that authenticates the user
fork icon0
star icon1
watch icon1

+ 8 other calls in file

How does validator.normalizeEmail work?

validator.normalizeEmail is a function in the validator library that normalizes an email address by converting it to lowercase and removing extra whitespace. When you call validator.normalizeEmail(), you pass in an email address as a parameter. The function then performs the following steps to normalize the email address: Convert the email address to lowercase: validator.normalizeEmail() converts all characters in the email address to lowercase to ensure consistency. Remove extra whitespace: validator.normalizeEmail() removes any extra whitespace characters (e.g., spaces, tabs) from the beginning and end of the email address, as well as any whitespace characters between the local part and the domain part of the email address. Remove dots from the local part of the email address: validator.normalizeEmail() removes any dots (.) from the local part of the email address, since dots do not affect the routing of the email message. Remove tags from the local part of the email address: validator.normalizeEmail() removes any tags (e.g., +label) from the local part of the email address, since they are not part of the actual address. validator.normalizeEmail() then returns the normalized email address as a string. This function is useful for tasks that require validating and normalizing email addresses in JavaScript, such as in web forms or user registration systems. Overall, validator.normalizeEmail provides a simple way to normalize email addresses in JavaScript using the validator library.

73
74
75
76
77
78
79
80
81
82

if (validationErrors.length) {
  req.flash('errors', validationErrors);
  return res.redirect('/login');
}
req.body.email = validator.normalizeEmail(req.body.email, { gmail_remove_dots: false });

passport.authenticate('local', (err, user, info) => {
  if (err) { return next(err); }
  if (!user) {
fork icon0
star icon0
watch icon1

+ 7 other calls in file

61
62
63
64
65
66
67
68
69
70
try {
  if (string) {
    let result = validator.trim(string);
    result = validator.unescape(result);
    result = validator.stripLow(result);
    result = validator.normalizeEmail(result);
    return result;
  } else {
    return null;
  }
fork icon0
star icon0
watch icon0

Ai Example

1
2
3
4
5
6
7
8
9
const validator = require("validator");

// Normalize an email address
const email = "   John.Doe+Label@example.com  ";
const normalizedEmail = validator.normalizeEmail(email);

// Log the original and normalized email addresses to the console
console.log(`Original email: ${email}`);
console.log(`Normalized email: ${normalizedEmail}`);

In this example, we use validator.normalizeEmail to normalize the email address 'John.Doe+Label@example.com'. We pass in the email address as a parameter to validator.normalizeEmail(). The function then normalizes the email address by converting it to lowercase, removing extra whitespace characters, and removing any dots or tags from the local part of the email address. It returns the normalized email address as a string. We then log both the original email address email and the normalized email address normalizedEmail to the console. Overall, validator.normalizeEmail provides a simple way to normalize email addresses in JavaScript using the validator library.

705
706
707
708
709
710
711
712
713
714
715
 */
exports.postForgot = async (req, res, next) => {
  if (!validator.isEmail(req.body.email)) {
    return res.send({ msg: "Please enter a valid email address." });
  }
  const email = validator.normalizeEmail(req.body.email, {
    gmail_remove_dots: false,
  });


  const token = crypto.randomBytes(16).toString("hex");
fork icon0
star icon0
watch icon0

+ 2 other calls in file

3
4
5
6
7
8
9
10
11
12
13
14
let email = "foo+bar@gmail.com";
let password = " fh";


if (validator.isEmail(email)) {
  errors.push({ message: "E-mail is invalid." });
  let ismail = validator.normalizeEmail(email);
  console.log(ismail);
}


if (
fork icon0
star icon0
watch icon0

67
68
69
70
71
72
73
74
75
76
  const error = new Error("Username alread exists, please change your username");
  error.code = 401;
  throw error;
}

const email = validator.normalizeEmail(validator.ltrim(userSignUpInput.email.toLowerCase()));

const name = validator.ltrim(userSignUpInput.name);
const hashedPw = await bcryptjs.hash(validator.ltrim(userSignUpInput.password), 12);
const userName = validator.ltrim(userSignUpInput.username);
fork icon0
star icon0
watch icon0

393
394
395
396
397
398
399
400
401
402
'removelinebreak': {
  converter: (val) => val.toString().replace(/(\r\n\t|\n|\r\t)/gm, '')
},
'normalizeemail': {
  method: 'isEmail',
  converter: V.normalizeEmail
},
'lowercase': {
  converter: (val) => val.toString().toLowerCase()
},
fork icon0
star icon0
watch icon0

1343
1344
1345
1346
1347
1348
1349
1350
1351
1352

    res.json({message: response})
});


We can use validator.normalizeEmail() function to remove 
formatting on email inputs to remove potentially dangerous characters. For example:

console.log(validator.normalizeEmail("     STUDENT@Codecademy.com"))
The above code will print out student@codecademy.com.
fork icon0
star icon0
watch icon1

+ 3 other calls in file