Programming,  Python

Google Cloud Function To Validate Email Addresses

The following is a simple Google Cloud Function that will validate that text entered into a field (passed to the function in the request body when called) is a valid format of an email address. The email address might not be real, but at least we aren’t accepting a string that’s incorrectly formatted:

exports.validateEmailAddress = functions.https.onRequest((req, res) => {

// Get the email address from the request body.

const emailAddress = req.body.emailAddress

// Validate the email address.

const isValid = /^[a-zA-Z0-9.!#/.test(emailAddress);

// If the email address is valid, return a success response.

if (isValid) { res.sendStatus(200); } else {

// If the email address is not valid, return an error response. res.sendStatus(400); } });

This function will validate the email address that is passed in the request body. If the email address is valid, the function will return a success response (200). If the email address is not valid, the function will return an error response (400).