0

I have set up some remote validation in my controller for email validation so that the email can be verified right away. I am hoping to take this a little bit further and validate the email as the user is still typing. Right now it validates when the user takes the focus off of the text box for the first time but if they go back to the text box, it will validate on a key up. Is there a way to set up remote validation with a different trigger? Maybe even a key up but after a certain amount of time has elapsed?

tereško
  • 58,060
  • 25
  • 98
  • 150
Pittfall
  • 2,751
  • 6
  • 32
  • 61
  • Possible duplicate: http://stackoverflow.com/questions/5407652/asp-net-remote-validation-only-on-blur – VahidN May 10 '12 at 18:46

1 Answers1

1

You can do this with a basic setTimeout in Javascript before running your validation code.

A quick example using jQuery:

$('#myElement').keyup(function(){
     setTimeout(ValidationFunction,1000);
});

If you need to clear the timeout when a key is pressed you can do:

var myTimeoutFunction;

$('#myElement').keyup(function(){
         myTimeoutFunction = setTimeout(ValidationFunction,1000);
    });

$('#myElement').keydown(function(){
     window.clearTimeout(myTimeoutFunction);
});

Here's a working example

Jamie Dixon
  • 53,019
  • 19
  • 125
  • 162
  • I'm not very experienced with Javascript, how do I call the timeout method from my email textbox? – Pittfall May 10 '12 at 14:45
  • This part `$('#myElement')` refers to a textbox with the id `myElement` (you'll name that as you need it). This code then registers the `keyup` event for that textbox and fires each time the user presses and releases a key, re-setting the timeout to 1 second. – Jamie Dixon May 10 '12 at 14:49
  • After 1 second the `myTimeoutFunction` function will fire which is where you'll put the logic required to validate the email address (some ajax code or somethning) – Jamie Dixon May 10 '12 at 14:51
  • Ok thanks for the example, I just have one more question. The validation method is in my controller, how do I specify which controller it's coming from? – Pittfall May 10 '12 at 15:34