[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 843
  • Last Modified:

Use both onkeyup and blur to run a function in jquery

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>');

}
});
0
gvilla23
Asked:
gvilla23
  • 4
1 Solution
 
leakim971PluritechnicianCommented:
Try :


$('#fields_lname').bind("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

0
 
leakim971PluritechnicianCommented:
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

0
 
leakim971PluritechnicianCommented:
0
 
gvilla23Author Commented:
Perfect!
0
 
leakim971PluritechnicianCommented:
Cool! Thanks for the points!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now