I want to validate the e-mail address entered by the user that it is like that format something@volt.gov must be writen in particular domain.how can I retrive it from the text box and check it?
If(Pattern.matches(('^[_A-Za-z0-9-]+@volt.[a-zA-Z]{2,4}$'), email))
{
}
Attribution to: Udaya
Possible Suggestion/Solution #1
Why not just use regular validation rules to do the job for you? Simply include an <apex:pageMessages/>
component on the page that's re-rendered when any actions take place, and you'll see the regular error messages generated by validation rules. This has the added advantage of checking the data regardless of where it's entered and standard options should always leveraged before leaping into custom code.
If you're not gather data directly into an SObject field but into a string controller variable (strAddress
in the example below), then a very basic check in the controller would look like the following, and again you'd use an <apex:pageMessages/>
tag in the page to display a message.
if(!strAddress.endsWith('@volt.gov'))
{
ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.Error, 'Invalid email domain used.');
return null;
}
else
{
// keep calm and carry on
}
Attribution to: Matt Lacey
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/31368