Search In This Blog

2016-09-17

How to validate email address with Regex expression in Visualforce page by Apex

When user input email address in Visualforce page, normally need to input the email address again as a confirm email address to make cross check.

If you create a field to save confirm email address. For example, this email address will be saved to Field(Email) in object(Contact), and use inputField tag will be convenient.
<apex:inputField value="{!Contact.Email}"/>
When you try to save, Salesforce will make the format check for you.

But if confirm email address is not a field in object, you may need to check format of confirm email address by Apex. Use Pattern.matches() will be a choice.
Regex foremail format check:
'^([0-9a-zA-Z+_.-]*[0-9a-zA-Z]+[@][0-9a-zA-Z+_.-]+\\.[0-9a-zA-Z+_.-]*)$'
*example email: huang.hai@randsfdc.blogspot.jp

Return the result of format check:
return Pattern.matches('^([0-9a-zA-Z+_.-]*[0-9a-zA-Z]+[@][0-9a-zA-Z+_.-]+\\.[0-9a-zA-Z+_.-]*)$', val); 
*val is the value of email address

To check are these two email address same, you can use String.equals(). Like: emailOne.equals(emailTwo).

String.equals(): Upper char and lower char will be treated as different char.

2 comments:

  1. At present most of the people don't have proper knowledge about Email List Verification so i think this website is very important for them. If you view this website then you will get lots of information about it. I hope you will be happy for their valuable information.

    ReplyDelete
  2. Yes i am totally agreed with this article and i just want say that this article is very nice and very informative article.I will make sure to be reading your blog more. You made a good point but I can't help but wonder, what about the other side? !!!!!!THANKS!!!!!! http://verifications.io

    ReplyDelete