gecko Posted May 27, 2009 Share Posted May 27, 2009 Hi, Im looking for a piece of javascript I can incorporate into some web forms I have got for adding student and staff records to a db. Basically, each form firstly requests for firstName and surname for the new user's record. Further down the form, it then asks for the user's email address. What I want to do is take the value entered in the FirstName field and the Surname field, concatenate them together to make an email address suggestion in the email field. So, if firstName was 'joe' and surname was 'bloggs', then the suggested email value could be = joe.bloggs@universityname.com Any ideas? I have written some stuff myself, but it isn't working for adding 2 values; at present, when the surname is completed, the surname value replaces the firstName value in the email field. Any ideas from someone more experienced with JS would be much appreicated!! Quote Link to comment Share on other sites More sharing options...
Axeia Posted May 27, 2009 Share Posted May 27, 2009 Would help if you posted what you have cause it sounds like you need to use += instead of = somewhere. Quote Link to comment Share on other sites More sharing options...
gecko Posted May 27, 2009 Author Share Posted May 27, 2009 I'ver deleted everything that I did for it, as it wasn't working - it was only adding one value at a time. So I thought I must have been doing somethi the lookout for anyone who may have done something similar for one of their sites Any suggestions would be great, as Im no expert Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 27, 2009 Share Posted May 27, 2009 This should get you started. Note that I would suggest adding some code to remove/replace characters that are invalid for an email. For example a last name like "De la hoya" or "O'Doyle" <html> <head> <script> function suggestEmail() { //Create references to the fields var fName = document.getElementById('fname'); var lName = document.getElementById('lname'); var eMail = document.getElementById('email'); //Set suggested email if firt & last names have value and email does not if (fName.value && lName.value && !eMail.value) { eMail.value = fName.value + '.' + lName.value + '@domainname.com'; } return; } // </script> </head> <body> <form name="myform"> First Name: <input type="text" name="fname" id="fname" onchange="suggestEmail();" /><br /> Surname: <input type="text" name="lname" id="lanme" onchange="suggestEmail();" /><br /> Email Address: <input type="text" name="email" id="email" /><br /> </form> </body> </html> Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 27, 2009 Share Posted May 27, 2009 Added some code to ensure a proper email is created. This will replce spaces with an underscore and will remove any invalid characters (not a-z, 0-9, underscore "_", dash "-" or period "."). <html> <head> <script> function suggestEmail() { //Create references to the fields var fNameObj = document.getElementById('fname'); var lNameObj = document.getElementById('lname'); var eMailObj = document.getElementById('email'); //Set suggested email if firt & last names have value and email does not if (fNameObj.value && lNameObj.value && !eMailObj.value) { var uname = fNameObj.value + '.' + lNameObj.value; uname = uname.replace(/[ ]/g, '_'); uname = uname.replace(/[^\w\d-\.]/gi, ''); eMailObj.value = uname + '@domainname.com'; } return; } // </script> </head> <body> <form name="myform"> First Name: <input type="text" name="fname" id="fname" onchange="suggestEmail();" /><br /> Surname: <input type="text" name="lname" id="lanme" onchange="suggestEmail();" /><br /> Email Address: <input type="text" name="email" id="email" /><br /> </form> </body> </html> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.