Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8177843
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T23:37:18+00:00 2026-06-06T23:37:18+00:00

I have a number of inputs that are added dynamically via a plus and

  • 0

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….

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T23:37:20+00:00Added an answer on June 6, 2026 at 11:37 pm

    You need to delegate for your dynamically created inputs
    if using jQuery 1.7+ then use on

    $(document).on('blur','#encdomlocal input:nth-child(2)',function() {
    

    Else you can use delegate jquery 1.6 and lower

    $(document).delegate('#encdomlocal input:nth-child(2)','blur',function() {
    

    document can be replaced with any parent element

    UPDATE:

    if (REGEX.test($(this).val())) { //<-- I don't know what you were testing but using $(this) works here
        $(this).removeClass();
        $(this).addClass("good");
    }
    else {
        $(this).removeClass();
        $(this).addClass("bad");
    }
    

    Here’s the updated fiddle http://jsfiddle.net/9DW8f/9/

    You were using

    if (REGEX.test($("#encdomlocal input:nth-child(2)").val()))
    

    it was returning a set of elements so you weren’t testing the current textbox that fired the blur event

    The jQuery Object: The Wrapped Set: Selectors return a jQuery object known as the “wrapped set,” which is an array-like structure that contains all the selected DOM elements. You can iterate over the wrapped set like an array or access individual elements via the indexer ($(sel)[0] for example). More importantly, you can also apply jQuery functions against all the selected elements.<

    If you are going to be using the same REGEX you can just add the other selector by separating with comma

    $("#encdomlocal").on("blur", "input:nth-child(2),otherSelector",//<--- use this if regex for validation is the same
    

    If you are using a new regex you can just use

    $(document).on('blur','otherSelector',function() {
        // validation code goes here.  You can pretty much copy the other one and change the regex to what you need
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wish to get the number of all TR elements that have a input
I'm writing a Java application that will have an on-screen number pad available for
I have a data frame that contains a number of fields. One of these
I have a form/table that is dynamically altered via JS by adding/removing rows (form
I have a number input that should trigger jQuery on every change. To do
I have a number of text input elements that when its values are changed,
I have 3 edittexts with input type as number and 1 button that say
I have several number inputs of varying lengths which I want to style such
Greetings, I have a form with a variable number of inputs, a simplified version
I have a textbox where a user inputs a number, like: 105.14 When I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.