Solved

Adding textboxes dynamically as needed

Posted on 2013-10-31
4
148 Views
Last Modified: 2013-11-07
I want users to start with one textbox and then add additional ones as needed.... this will be used on one scenario for email addresses and another for license keys as an example of usage.

So they add their first email address, with validation. Then below it is a link:
+ Add Another Email Address

Then finally the [Submit] button.

On press of the Add Another Email address, another box appears and so forth until they have all they need.

I have searched the internet and only found one bit of source code on it. It's close, but has some problems and is klunky.
<link to competing site deleted by COBOLdinosaur, Topic Advisor>

I also found this, but it's not pretty:
http://viralpatel.net/blogs/dynamic-add-textbox-input-button-radio-element-html-javascript/

I want it to say:
Email Address: [textbox]
[with validation]

Does anyone have any better suggestions for how to do this and how to read back the data on Submit? I don't care if it's asp.net or javascript as long as it's instant and works well. And I can create it and retrieve the results easily.

thanks!
0
Comment
Question by:Starr Duskk
  • 2
4 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 39615574
Let me know if the following code need to be documented : http://jsfiddle.net/QxUwy/

window.onload = function() {

    document.getElementById("add_email").onclick = function() {

        var email = document.createElement("input");
        email.type = "email"; // or text
        email.name = "email";

        var linebrk = document.createElement("br");

        var emails = document.getElementById("emails");
        emails.appendChild(email);
        emails.appendChild(linebrk);
        return false;
    }
    
    document.getElementById("myform").onsubmit = function() {
        var emails =  document.getElementsByName("email");
        var number_of_emails = emails.length;
        var all_emails_valid = true;
        for(var i=0;i<number_of_emails;i++) {
            var currentEmail = emails[i];
            current_is_valid = validateEmail(currentEmail.value);
            all_emails_valid = all_emails_valid && current_is_valid;
        }
        if(!all_emails_valid) alert("one of the email is not valid!");
        return all_emails_valid;
    }
}

function validateEmail(value) {
 return /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))$/i.test(value);
}

Open in new window

0
 
LVL 2

Author Comment

by:Starr Duskk
ID: 39615657
Leakim,

That looks interesting, but I don't know how to retrieve it server side so I can save the values. Do you know how?

thanks.
0
 
LVL 82

Expert Comment

by:leakim971
ID: 39615708
Like this :
string[] emails = Request["email"].Split(',');

Open in new window

0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question