Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have created a visualforce page where the chatter functionality will be given access to the external members of an organisation by entering an external agency email address. I have put validations to check the contents of the email field. For example if I have put email address as abc it throws an error as "enter email address in the correct format". But if I try to save the email address as blank space it gets saved and a blank space user is shown in the pending list of members.

share|improve this question
Perhaps you can post a snippet of your code to help us understand how you have built the page. I can imagine one possibility of what might be causing this problem, but it would be easier if I knew what your VF page contained. Also, it would help to know what the underlying datatype of the field was. Typically, with, you shouldn't have to validate email entries if your field is the correct data type. – Peter Jan 22 '13 at 16:05
Agreed, the regex to check if an email is valid is actually fairly enormous: So you should just bind your vf input to an email field and have sf do it for you. – Greg Grinberg Jan 22 '13 at 16:34
To follow on from grigriforce's comment and the related link, I've come to the conclusion that the only real validation that you should perform on email address fields is that they contain an "@" and perhaps get a user to enter twice to reduce mis-spellings. Anything else is almost certainly guaranteed to not be correct unless you do the insane RFC922 validation which honestly is only slightly better than testing for "@". – jkraybill Jan 23 '13 at 3:00
Ohk.Let me first explain you the scenario,in the vf page there are two sets of objects, one is internal members of the organisation and another external members given chatter access through email I cannot make the external email field mandatory!! The second one is creating the problem... – MaxPyne Jan 23 '13 at 9:36
Here is the code snippet...let me know if any changes are needed in th code!! public string setEmailAdd {get;set;} public static String validEmailOrBlank(String setEmailAdd) { if (setEmailAdd != null || setEmailAdd='' ) system.debug('Enter a email ADDRESS'+setEmailAdd); String emailRegex = '([a-zA-Z0-9_\\-\\.]+)@((\[a-z]{1,3}\\.[a-z]{1,3}\\.[a-z]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.‌​)....Regex & Email validations!! system.debug('Enter a email Address'+setEmailAdd); if (!emailMatcher.matches()) { setEmailAdd = ''; } } else return null; return setEmailAdd; } – MaxPyne Jan 23 '13 at 9:46

For that also you need to keep a condition

say Suppose

var email = $(".email").val();
if(email=='' || email==null){
    alert("Enter a email Address");
    return false;

If u put a condition like that then it will throw a error message

share|improve this answer
adding &nbsp will work ?? – MaxPyne Jan 22 '13 at 12:03
no.You have to keep a condition for it – Eagerin Sf Jan 22 '13 at 12:03

If you are using apex:inputText component, you can use the "required" attribute to make it mandatory to a user to enter a value.

share|improve this answer

If you are using apex input text you can use the below apex code using regular expressions

public static boolean isUserEmailValid(String userEmail) {
        Boolean flag = false;
        if(userEmail != null) {
            String  emailRegex      = '([a-zA-Z0-9_\\-\\.]+)@((\\[a-z]{1,3}\\.[a-z]{1,3}\\.[a-z]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})';
            Pattern emailPattern    = Pattern.compile(emailRegex);
            Matcher emailMatcher    = emailPattern.matcher(userEmail.trim());
            flag                    =  emailMatcher.matches();
        return flag;
share|improve this answer
I have tried with the code...but the problem still remains.Its taking spaces!! – MaxPyne Jan 22 '13 at 13:51
Before passing value to this code please check that the given value is not null and empty and then pass to it – Ugesh Gali Jan 22 '13 at 14:01
FYI, this regex is suitable for situations where users are expected to have "normal" email addresses, but there are many many email addresses that are valid that will fail against this regex. See the link that grigriforce posted for the RFC-compliant regex for validating email addresses. I don't recommend using regexes for email address validation. – jkraybill Jan 23 '13 at 3:03

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.