I'm facing an email validation challenge for my project. I want to throw an error if any of the following scenarios are met:

  1. The email has numbers at the end, like '[email protected]'.
  2. The email contains the '&' character, like 'email2&[email protected]'.
  3. The email includes the '/' character, like 'email3/[email protected]'.

Could anyone kindly guide me on how to implement this validation in my code? Your assistance would be greatly appreciated! Thank you in advance.

2 Answers 2


This can be done in a validation rule with the following formula:

CONTAINS(My_Field__c, '/') || CONTAINS(My_Field__c, '&') || ISNUMBER(RIGHT(My_Field__c, 1))
  • The first two conditions check whether your field contains a / or a &.
  • The last condition gets the last character (using RIGHT) and then checks if it is a number using ISNUMBER
  • 1
    Thank you very much Sir
    – HSD
    Aug 23 at 13:15
// Define a method that takes an email as a parameter and returns a boolean value
public static Boolean validateEmail(String email) {
        // Declare a variable to store the regular expression pattern for a valid email
        String pattern = '^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}$';
        // Create a Pattern object using the pattern
        Pattern p = Pattern.compile(pattern);
        // Create a Matcher object using the email and the pattern
        Matcher m = p.matcher(email);
        // Return true if the email matches the pattern, false otherwise
        return m.matches();

// Define a method that takes a list of emails as a parameter and throws an exception if any of them is invalid
public static void checkEmails(List<String> emails) {
        // Loop through the emails
        for (String email : emails) {
                // Call the validateEmail method and store the result
                Boolean isValid = validateEmail(email);
                // If the result is false, throw an exception with the invalid email
                if (!isValid) {
                        throw new EmailException('Invalid email: ' + email);

// Define a custom exception class for invalid emails
public class EmailException extends Exception {}

This code will create two methods: one to validate a single email using a regular expression, and another to check a list of emails and throw an exception if any of them is invalid.

The regular expression will reject any email that has numbers at the end, contains the '&' character, or includes the '/' character, as per your scenario.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .