I have a number of inputs that are added dynamically via a plus and a minus sign.
All of these inputs will be added within the div encdomlocal. Im using the following code to validate the input and then add the css, but its not working as expected. i.e it only validates the first input box within the div.
$('#encdomlocal input:nth-child(2)').blur(function() {
var REGEX = /^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/;
if (REGEX.test($("#encdomlocal input:nth-child(2)").val())) {
$(this).removeClass();
$(this).addClass("good");
}
else {
$(this).removeClass();
$(this).addClass("bad");
}
});
Any help would be great, thanks….
You need to delegate for your dynamically created inputs
if using jQuery 1.7+ then use on
Else you can use delegate jquery 1.6 and lower
documentcan be replaced with any parent elementUPDATE:
Here’s the updated fiddle http://jsfiddle.net/9DW8f/9/
You were using
it was returning a set of elements so you weren’t testing the current textbox that fired the
blureventIf you are going to be using the same REGEX you can just add the other selector by separating with comma
If you are using a new regex you can just use