Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Adding textboxes dynamically as needed

Posted on 2013-10-31
4
Medium Priority
?
168 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
3 Comments
 
LVL 83

Accepted Solution

by:
leakim971 earned 2000 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 83

Expert Comment

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

Open in new window

0

Featured Post

Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

Question has a verified solution.

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

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
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…
Suggested Courses
Course of the Month20 days, 18 hours left to enroll

810 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