9 Replies Latest reply on Apr 7, 2011 8:34 PM by George_Johnson

    Creating An Email Address From A Text Field

    complete_noob

      We use the standard first letter of a person first name + their last name + @ + domain name for email addresses. Is it possible for me to calculate the email address from a text field containing the first and last names of a user?

       

      i.e. text field contains "John Smith" = jsmith@abc.com

       

      Many thanks for your time...it is appreciated.

        • 1. Re: Creating An Email Address From A Text Field
          George_Johnson MVP & Adobe Community Professional

          Sure. A custom calculate script for a text field might look like:

           

          (function () {
          
              //Get the field values
              var v1 = getField("name_first").valueAsString;
              var v2 = getField("name_last").valueAsString;
          
              // Blank this field if either of the inputs are blank
              if (!v1) {
                  event.value = "";
                  return;
              }
          
              if (!v2) {
                  event.value = "";
                  return;
              }
          
              // Set this field value
              event.value = (v1[0] + v2 + "@example.com").toLowerCase();
          
          })();
          

           

          Change the names of the input fields to match what you're using.

          • 2. Re: Creating An Email Address From A Text Field
            complete_noob Level 1

            George Thank you so much for your reply.

             

            My problem is that the name field contains both the first and last name - they are not separate fields.

             

            Any ideas?

             

            Thanks again,

             

            complete_noob

            • 3. Re: Creating An Email Address From A Text Field
              George_Johnson MVP & Adobe Community Professional

              It would be best if you did make them separate fields, but you can split the full name string at spaces in an attempt to get the first and last name individually. For example:

               

              var v1, v2
              var name_full = getField("name_full").valueAsString;
              var name_parts = name_full.split(" ");
              
              if (name_parts.length === 2) {
                  v1 = name_parts[0];
                  v2 = name_parts[1];
              } else {
                  // String did not plit into two separate strings
                  // so we should not proceed
                  event.value = "";
                  return;
              }
              
              // Contine with the rest of the script here
              
              • 4. Re: Creating An Email Address From A Text Field
                complete_noob Level 1

                George, it is very kind of you to respond!

                 

                I still have to pull off the 1st letter of the first name and then all of their last name, combine the two pieces and add the

                @xyz.com.

                 

                Being a noob it looks like you were able to split the name but, not isolate off the first leter of the first name. Did I miss it?

                 

                Thanks again George!

                • 5. Re: Creating An Email Address From A Text Field
                  George_Johnson MVP & Adobe Community Professional

                  That's what this line does:

                   

                      event.value = (v1[0] + v2 + "@example.com").toLowerCase();
                  
                  
                  • 6. Re: Creating An Email Address From A Text Field
                    complete_noob Level 1

                    So the total script looks like this?

                     

                    ar v1, v2

                    var name_full = getField("name_full").valueAsString;

                    var name_parts = name_full.split(" ");

                    if (name_parts.length === 2) {

                        v1 = name_parts[0];

                        v2 = name_parts[1];

                    } else {

                        // String did not plit into two separate strings

                        // so we should not proceed

                        event.value = "";

                        return;

                    }

                    (function () {

                        //Get the field values

                        var v1 = getField("name_first").valueAsString;

                        var v2 = getField("name_last").valueAsString;

                        // Blank this field if either of the inputs are blank

                        if (!v1) {

                            event.value = "";

                            return;

                        }

                        if (!v2) {

                            event.value = "";

                            return;

                        }

                        // Set this field value

                        event.value = (v1[0] + v2 + "@bushnell.com").toLowerCase();

                    })();

                     

                     

                    Thanks again George!

                    • 7. Re: Creating An Email Address From A Text Field
                      George_Johnson MVP & Adobe Community Professional

                      No, you need to replace the old with the new:

                       

                      (function () {
                      
                          var v1, v2
                          var name_full = getField("name_full").valueAsString;
                          var name_parts = name_full.split(" ");
                      
                          if (name_parts.length === 2) {
                              v1 = name_parts[0];
                              v2 = name_parts[1];
                          } else {
                              // String did not split into two separate strings
                              // so we should not proceed
                              event.value = "";
                              return;
                          }
                      
                          // Set this field value
                          event.value = (v1[0] + v2 + "@bushnell.com").toLowerCase();
                      
                      })();
                      

                       

                      Note that this is really just intended as a starting point. You should do more checking of the input and figure out what to do with names like "Oscar de la Renta", "John Paul Jones", etc.

                      • 8. Re: Creating An Email Address From A Text Field
                        complete_noob Level 1

                        I think the light is starting to shine!

                         

                        Would this be entered as a mouse up event?

                        • 9. Re: Creating An Email Address From A Text Field
                          George_Johnson MVP & Adobe Community Professional

                          No, it is designed to be a custom calculate script for the field that shows the email address.