+ Start a Discussion
vandana rajuvandana raju 

call apex method on keypress event

Hello
I having a custom object C1 with fields like name,email and mobile number.On an visualforce page I will enter the values and save the data.This part is fine with me.
Now my requirement is when I enter email in <apex:inputfield> say on keypress event I should be able to call an server side method in apex controller.
This method will check if the email already exists in object. If it exists then some action needs to be taken.

Please anybody any help will be greatly appreciated
thanks
vandana
 
LBKLBK
Hi Vandana,

OnKeyPress may not be the suitable event for you to validate the Email address.

You have to use onblur event of the <apex:inputfield> component and setup an apex:actionFunction to make a callback to your APEX method.

Your code will approximately look like this.
VF Page
<apex:page controller = "myController">
	<apex:form>
		<apex:actionFunction action="{!validateEmail}" name="fnValidateEmail">	
		</apex:actionFunction>
		
		<apex:inputField value="{!contact.Email}" onblur="fnValidateEmail" />
	</apex:form>
</apex:page>

/*****Controller*****/
public class myController {
	public PageReference validateEmail() {
		//do your validation here
		return null;
	}
}
Hope this helps.
 
sunny522sunny522
Hey Vandana Raju,
     Here is simple example to call action:function
http://salesforceglobe4u.blogspot.com/2017/05/actionfunction-in-salesforce.html
Let me know if u need any further help.