4

I need validate the dynamically appended input text box for the below code

$("#btnadd").click(function() {
             // Made it a local variable by using "var"
            var addkey = document.getElementById("txtaddkey").value;
            if(addkey!=""){
                $('<li><span>'+addkey+'</span><span class=\"amountin\"><a href=\"#\">$0.05</a> $ <input type=\"text\" maxlength=\"5\"/></span><span class=\'close ui-icon \'></span></li>')
                    .find('.close').click(function (){
                        $(this).parent().remove();  
                    })
                    .end().appendTo('#keyword');
                $('#txtaddkey').val('');
            }
        });

2 Answers 2

2

I think this might help JQuery validate dynamically add rules

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you Aaron... As you suggested I have checking class name for dynamic input box validation.
@Elankeeran ~ Since it's not obvious to me how his answer helped you, perhaps either you or @AaronHathaway can spell out the obvious "look at this part" for the next person to come along who would be equally helped by this answer.
your link gives the idea to use class for validation rule. But drachenstern your answer for common validation for jquery events.
1

How to add jQuery anything handling to a dynamically appended element

http://api.jquery.com/live/ - Live was designed for just this thing

Given an appended textbox of <input type='text' id='mytextbox'></input> then this would be helpful: ( NOTE: I recommend you add an ID to the textbox if it will only show up once, or else a useful classname if it will be added repeatedly ~ Update the selector below to reflect the ID or class)

$('#mytextbox').live('keyup', function(event){ 
  if ( $(this).val() == "fail value" ) {
    $(this).val() = "good value";
  }
});

It really depends on how you want to validate it. The initial question doesn't really show how you would validate it now, (unless I'm missing something)

3 Comments

I am looking for general form validation for validate the dynamic input text box on form submit but in your solution it will work only on key up cases.
@Elankeeran ~ I think you absolutely do not understand my example. I was offering an example! not a final solution. You could alter that to be 'keyup keydown keypress' and get all three events, but how would you know which event you were handling? You must read the jQuery api reference for this answer.
@Elankeeran ~ Also note that had you given me what you wanted to have for a validation (which you did not) I could have incorporated that into my answer. I suggest stackoverflow.com/questions/how-to-ask

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.