+ Start a Discussion
Jimmy TrinhJimmy Trinh 

Visualforce onblur will not enter controller method

Hi,

I am trying to set up a validation method when the user finishes typing and leaves the inputText.  I am grabbing that input and passing it to the controller extension and then grab a list of all the contact's emails and comparing it to the input to see if there is a match.  When I press the submit button to save a record, the apexclass will go into the validation method.  It just won't enter the method using onblur for some reason.  I am just wondering if there are any clues to as why this may be?

Also this messes up with some of the fields I am rerendering, even though email is not a condition in the rerender statement things will not show up unless I type an email.

Below is the javascript calling the validation method within the apex class:
    <script type="text/javascript">
        function getEmail(selectedValue){
            alert('Inside the javascript the email is: ' + selectedValue.value);
            ValidateEmail(selectedValue.value);
            return false;
        }    
    </script>

Below here is the inputText line with the onblur method:
<apex:inputText value="{!emailid}" styleClass="form-control" style="text-align: left;width:45%" required="true" onblur=" getEmail(this);" />
        <apex:actionFunction action="{!ValidateEmail}" name="ValidateEmail">        
            <apex:param id="emailid" name="emailid" value="" assignTo="{!emailid}"/> 
        </apex:actionFunction>  

Then below is the method within the apex class:
public void ValidateEmail(){
        System.debug('Just entered ValidateEmail');
        List<Contact> contactList = [SELECT Email FROM Contact];
        String tmp = Apexpages.currentPage().getParameters().get('emailid');
        Boolean match = false;
        
        for(Contact c: contactList)
        {
            if(emailid == c.Email){
                System.debug('There is a match');
                System.debug('tmp email: ' + tmp);
                System.debug('emailid: ' + emailid);
                System.debug('list email: ' + c.Email);
                match = true;
                break;
            }
        }
        if(match == false){
            System.debug('There is no match');
            ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.FATAL, 'Email not in database please enter valid email');        
        }
    }

Thanks!
Best Answer chosen by Jimmy Trinh
Magesh Mani YadavMagesh Mani Yadav
Hi Jimmy,

I have updated your code. Just try the below class and Vf page. Should work for onblur event.
public class sampleTest {

    public sampleTest(){
        
    }
    public String emailid{get;set;}
    
    public void ValidateEmail(){
        System.debug('Just entered ValidateEmail');
        List<Contact> contactList = [SELECT Email FROM Contact];
        //String tmp = Apexpages.currentPage().getParameters().get('emailid');
        Boolean match = false;
        System.debug('emailid: ' + emailid);
        for(Contact c: contactList)
        {
            if(emailid == c.Email){
                System.debug('There is a match');             
                System.debug('list email: ' + c.Email);
                match = true;
                ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Info, 'Match found')); 
                break;
            }
        }
        if(match == false){
            System.debug('There is no match');
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL, 'Email not in database please enter valid email'));        
        }
    }
}
 
<apex:page controller="sampleTest">
    <apex:form id="theForm">
        <apex:pageMessages />
       <apex:inputText value="{!emailid}" styleClass="form-control" style="text-align: left;width:45%" required="true" onblur=" getEmail();" />
        <apex:actionFunction action="{!ValidateEmail}" name="ValidateEmail" reRender="theForm">   
        </apex:actionFunction> 
    </apex:form>
    <script type="text/javascript">
    function getEmail(){
        ValidateEmail();
        return false;
    }    
    </script>  
</apex:page>
Hope the issue is resolved