You are viewing limited content. For full access, please sign in.

Question

Question

add functions to email form object

asked on October 17, 2023

I have a need to provide a default value in a form when a value in a different field is selected.  I have the function working fine in 2 text objects on the form, however, the 3rd (and most important) object is an email form object, which does not have the the formula option in the advanced tab. 

I see a possible solution as moving from an email variable to a text variable, but then I need to come up with js validation for other e-mails that are not impacted by the script (i have a behavior that has to happen to several departments, but not all.) 

 

So if someone knows how to add a function to an email form object, that solves it for me - or id someone can point me to an example of doing e-mail validation on a text field, I am good.  I am open to other approaches.

 

 

0 0

Replies

replied on October 17, 2023

Since you tagged this as Version 10, we know you are using the Classic Designer, and thus Javascript is going to be a valid option.

Here's an example of some code that populates an email into an email field that has class name of myEmailField based on the selections in a drop-down field that has class name of myDropdownField.

//Note that this code assumes that there is only a single field with the
//myDropdownField class name - it won't work as expected if there is more
//than one.

$(document).ready(function() {
  
  //Make the department email field readonly.  Doing it here instead of
  //on the Layout page means it will allow changes via code.  If it's 
  //readonly from the Layout page, the changes won't save.
  $('.myEmailField input').attr('readonly', 'true');
  
  //When the Department dropdown changes, populate the department email field.
  $('.myDropdownField select').change(function() {
    var dropdownSelect = $(this).val();
    if (dropdownSelect == 'Accounting') {
      $('.myEmailField input').val('accounting@fakeaddress.com');
    }
    else if (dropdownSelect == 'Finance') {
      $('.myEmailField input').val('finance@fakeaddress.com');
    }
    else if (dropdownSelect == 'Marketing') {
      $('.myEmailField input').val('marketing@fakeaddress.com');
    }
    else if (dropdownSelect == 'IT') {
      $('.myEmailField input').val('it@fakeaddress.com');
    }
    else if (dropdownSelect == 'Management') {
      $('.myEmailField input').val('management@fakeaddress.com');
    }
  });
  
});

If you want to share specifics of the fields/options you are using and the results you need, I can provide a more tailored Javascript solution.

0 0
replied on October 17, 2023

Thanks for this Answer - however out of the 50+ departments I have, only one has a default value that needs to be populated.  I had an answer from a coworker that i think will work best.  I am moving the field to a text field so that I can apply my formula and then I am adding a regex to validate the e-mail 

 

 

.*@.*\..{2,3}

 

 

0 0
replied on October 17, 2023

That will definitely work for most cases.

 

I suggest a slightly more robust RegEx, such as:   

^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$

Source: https://regexr.com/3e48o

 

Or this one: 

\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}\b

Which I would actually tweak to this for use in LFForms and to allow lowercase:
^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$

Source: https://www.regular-expressions.info/email.html

 

There are domain names like .info that wouldn't work with the RegEx you listed, and it's allowing things like spaces and other weird characters, that these two examples will handle a little better.

Bear in mind that even these examples are not going to be able to match every possible kind of email address, RegEx to get that is pretty complex (example: see the very bottom of this page: https://www.regular-expressions.info/email.html)...

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.