0

I'm having a business requirement of searching special characters in Email Field(before @domain.com)in a trigger.

For Ex: if a email is like this (t#@[email protected]).it should be able to identify the'#@' and make that email field blank.Is there any way available to identify the special characters before '@gmail.com'.

3
  • Use regular expression..Using that you can Identify it Mar 23, 2018 at 7:28
  • THanks ..I'm using the patter class to identify Characters.
    – Rishi
    Mar 23, 2018 at 7:30
  • system.debug('@@@'+pattern.matches('[a-zA-Z0-9.]+@[a-zA-Z.-]+.[a-zA-Z]{2,4}', 'te@[email protected]'));
    – Rishi
    Mar 23, 2018 at 7:30

1 Answer 1

3

Use String.containsNone() to check whether a string contains any prohibited characters, or String.containsOnly() to check that it only contains a specific set of characters. That said, be aware that email addresses are a good deal more flexible than you might think, and special characters aren't necessarily invalid.

Regular expressions are generally not a good fit for validating email addresses, which is surprisingly tricky. There's a lot of edge cases that are perfectly valid but that your regex in the comments will not match. Your regex, for example, won't match [email protected], which is not only valid but common, or [email protected]. (There are now a lot of TLDs, not all of which are 2-4 characters long).

For more on why parsing and validating email addresses is really hard, and most validation is too strict or just plain wrong, see for example this article, or read the relevant RFCs 822, 2821, 2822, 3696.

You must log in to answer this question.

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