We help IT Professionals succeed at work.

Use both onkeyup and blur to run a function in jquery

Medium Priority
852 Views
Last Modified: 2013-11-19
I need to run this function in jquery with keyup() and blur(). I can get it to run with only one of them.
but not both.

$('#fields_lname').blur(function() {
$('span.error-lname').remove();
var inputVal = $(this).val();
var fakeReg = /(.)\1{2,}/;
if(fakeReg.test(inputVal)) {
$(this).after('&nbsp; <span class="error error-lname"><img src="images/check-mark-no.png" width="20" height="20" /></span>');
}else if ($(this).val() ==""){
$(this).after('&nbsp; <span class="error error-lname"><img src="images/check-mark-no.png" width="20" height="20" /></span>');
}else{
$(this).after('&nbsp; <span class="error error-lname"><img src="images/check-mark-yes.png" width="20" height="20" /></span>');

}
});
Comment
Watch Question

Multitechnician
CERTIFIED EXPERT
Distinguished Expert 2019
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
leakim971Multitechnician
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
or if #fields_lname is added dynamically :


$('#fields_lname').live("keyup blur", function() {
			$('span.error-lname').remove();
			var inputVal = $(this).val();
			var fakeReg = /(.)\1{2,}/;
			if(fakeReg.test(inputVal)) {
				$(this).after('&nbsp; <span class="error error-lname"><img src="images/check-mark-no.png" width="20" height="20" /></span>');
			} else if ($(this).val() ==""){
				$(this).after('&nbsp; <span class="error error-lname"><img src="images/check-mark-no.png" width="20" height="20" /></span>');
			} else{
				$(this).after('&nbsp; <span class="error error-lname"><img src="images/check-mark-yes.png" width="20" height="20" /></span>');
			}
		});

Open in new window

leakim971Multitechnician
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:

Author

Commented:
Perfect!
leakim971Multitechnician
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
Cool! Thanks for the points!
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.