CMS Development

CandiceLloyd
Participant

Blocked Email domains on form submission

Hi there,

 

I was hoping someone could provide some assistance, please.

We currently have 600+ forms and block email domains via the checkbox and also via the additional ones I add into the form field.

 

Our domain list constantly grows by a month to month basis and having to update these 600+ forms individually each time we have a new domain to block has become very tedious, time-consuming and soul-crushing.

 

Is there a better way I can go about this?

The people with these domains should not be allowed to submit the form nevermind making it onto our contact database.

 

Kind regards,

Candice

0 Upvotes
17 Replies 17
Kevin-C
Recognized Expert | Partner
Recognized Expert | Partner

Blocked Email domains on form submission

Hey @CandiceLloyd 

 

From my understanding the domains are blocked at the form level and not portal level, as you said. @MFrankJohnson had a solution in this older post on how he handles this, using a workflow as a triage point.

Kevin Cornett - Sr. Solutions Architect @ BridgeRev
0 Upvotes
CandiceLloyd
Participant

Blocked Email domains on form submission

Hey @Kevin-C 

Thank you for your feedback!

I did work through the article earlier this morning, the one with the workflow but unfortunately, that would not be an option for us as it would still allow the domain(s) in question to submit a form and either register or potentially gain access to something.

 

Do you think there would be a way to make use of a HubDB table with the Domain data added to the table and then I assume using a mixture of HubL and JavaScript be able to customize it to filter those domains out and not allow the submission to go through?

Even if that means we will have to add code to the pages that contain the form, I think it will be an easier way around it then manually updating these forms.

 

Kind regards,

Candice

0 Upvotes
Kevin-C
Recognized Expert | Partner
Recognized Expert | Partner

Blocked Email domains on form submission

Hey @CandiceLloyd 

 

You know what you might have figured it out!

 

You could create a varible that contains all the domains you with to block.

Then write a script that loops through those values on change as a vaildation process.

If it fails that validation prevent the form from being submitted, and possibly even remove the submit eventlistener.

 

We are actually looking at creating a repo on our site that would gate content and I'm sure we wouldn't want to just allow the competition to have it. This is a great usecase and I'd love to workwith and follow you through this case!

 

 

Kevin Cornett - Sr. Solutions Architect @ BridgeRev
0 Upvotes
CandiceLloyd
Participant

Blocked Email domains on form submission

Hey @Kevin-C 

 

Thank you for the feedback, glad to hear we may have a possible solution!

It would be great to be able to work on this together, I for one do not have much knowledge in this department, so learning as we go on and taking snippets from previously set up pages and forums.

 

So far this is what I have:

<script>
$(document).ready(function() {
$("input[type=email]").mouseleave(function() {
var inputEmail = $("input[type=email]").val()
console.log(inputEmail)
});
});
</script>

{% set form_domain = hubdb_table_rows(2616498, queryparam) %}

{% for row in form_domain %}

{% if row.name == inputName %}
#disable button
#prompt validation
{% endif %}

{% endfor %}


I am getting the value of the email input via javascript, then checking if that value is present in my DataBase. After that, I will write the logic to disable the button and prompt a validation message. 

When I run my JS in the console, it works fine, when I publish my JS to the template, it doesn't seem to work. 

Any suggestions, or forums you can point me to?

 

Kind regards,

Candice

 

 

0 Upvotes
Kevin-C
Recognized Expert | Partner
Recognized Expert | Partner

Blocked Email domains on form submission

Aweseome @CandiceLloyd 

Ill take a look at this in a few hours! Lookin forward to it.

Kevin Cornett - Sr. Solutions Architect @ BridgeRev
0 Upvotes
Kevin-C
Recognized Expert | Partner
Recognized Expert | Partner

Blocked Email domains on form submission

Hey @CandiceLloyd 

 

So I did some digging and just completed a brief test this morning. And I think we've got a good start.

 

Working with code from this article, we can bypass the form action(post), run our validation, and finally trigger the form submission. This does get a 415 error meaning the server rejected the submission, but my contact list says otherwise…we'll have to dive into that later. Or maybe someone with more experience in the area can help out!

 

Ill have some code up at somepoint today!

Kevin Cornett - Sr. Solutions Architect @ BridgeRev
0 Upvotes
Kevin-C
Recognized Expert | Partner
Recognized Expert | Partner

Blocked Email domains on form submission

First stab at this:

$(document).ready(function(){
	const blockedDomains = ['verizon.com', 'att.net', 'hotmail.com'];

	var checkExist = setInterval(function() {
		if ($('form').length) {
			console.log("Exists!");
			clearInterval(checkExist);

			var ourForm = $('form');
			ourForm.on('submit', function() {

				// Prevent Submission
				preventDefault();

				// Assign varible to email field value
				var email = ourForm.find('input[type=email]').val();
				// Extract the email's domain
				var emailDomain = email.slice( ( email.indexOf('@') + 1 ), email.length );
				// Test if the domain is in the blocked domains
				var resp = $.inArray( emailDomain, blockedDomains ) ? true : false;

				// Handle the response of our validation above
				if (resp) {
					// If resp is true
					ourForm.submit();
				} else {
					// Else do nothing
					return
				}

			});
		}
	}, 100); // check every 100ms

    // Call checkExists
    checkExist();
});
Kevin Cornett - Sr. Solutions Architect @ BridgeRev
0 Upvotes
CandiceLloyd
Participant

Blocked Email domains on form submission

Hey @Kevin-C 

 

Wow, this is great, what a good start you have going here!

I finish up my workday in the next 40min and then I will dive back into this.

 

Thank you for all your assistance so far, I really appreciate all the help and input.

 

Kind regards,

Candice

CandiceLloyd
Participant

Blocked Email domains on form submission

Hey @Kevin-C 

 

I worked through your code last night.

From using sections of your code and other sources I found, I manage to get this working in a test environment with the following code:

var rejectList = [ "deny.com" , "reject.net" ];

//Trigger function on form submit
$("form").submit(function(e) {

//Getting the value from email field
var emailValue = $("input[type=email]").val();
alert(emailValue);
//removes name from email domain
var splitArray = emailValue.split("@");
 
//Checks if domain is in rejectList
if(rejectList.indexOf(splitArray[1]) >= 0) {
alert("Noo");
//prevents form from being submitted
e.preventDefault();
//add validation in here
}
 
});


I then now this morning tried to integrate/call the HubDB database by using this code:


<script type="text/javascript">

//HubDB domain list

var domains = [];
{% for row in hubdb_table_rows(2616498, queryparam) %}
domains.push({
"domains" : "{% if domains %}{{ row.domains|escapejson }}{% endif %}",
)};
{% endfor %}




//list of domains you want blocked
//REF: https://www.saltedstone.com/blog/how-to-build-a-store-locator-page-with-hubdb
var rejectList = [ "deny.com" , "reject.net" ];

//Trigger function on form submit
$("form").submit(function(e) {

//Getting the value from email field
var emailValue = $("input[type=email]").val();
alert(emailValue);
//removes name from email domain
var splitArray = emailValue.split("@");

//Checks if domain is in rejectList
if(rejectList.indexOf(splitArray[1]) >= 0) {
alert("Incorrect email address");
//prevents form from being submitted
e.preventDefault();
//add validation in here
}

});


</script>

And it does not seem to work!

 

There is a link in the above code labeled as REF.
This is what I used this morning to add the HubDB table.

 

I am going to spend more time on this after I finish work for the day.

 

Kind regards,

Candice

Kevin-C
Recognized Expert | Partner
Recognized Expert | Partner

Blocked Email domains on form submission

Hey @CandiceLloyd I think I've got something that works!

 

HubDB Table:

 

ID blocked_domain
12345 gmail.com
23456 aol.com

 

The Hubl:

<script>
window.blockedDomains = [ {% for row in hubdb_table_rows(**table #**) %} '{{row.**column_name**}}', {% endfor %} ]; </script>

The JS:

$(document).ready(function(){
	const blockedDomains = window.blockedDomains;

	$('form').on('submit', function(e) {

		// Assign varible to email field value
		var email = $('form').find('input[type=email]').val();
		// Extract the email's domain
		var emailDomain = email.slice( ( email.indexOf('@') + 1 ), email.length );
		// Test if the domain is in the blocked domains
		($.inArray( emailDomain, blockedDomains ) > -1) ? e.preventDefault() : void(0);

	});
});

 

Kevin Cornett - Sr. Solutions Architect @ BridgeRev
0 Upvotes
CandiceLloyd
Participant

Blocked Email domains on form submission

Hey @Kevin-C 

 

Thank you so much for this! It is exciting to see that we are getting somewhere.

 

On testing the code on my landing page, the database information is being pulled through to the blocked domains array.

For some reason, the remainder of the script is not firing. I have console.log() the email var and it seems that this is the origin of the problem. Do you have any idea why? or what else I can try?

$(document).ready(function(){
const blockedDomains = window.blockedDomains;

$('form').on('submit', function(e) {

// Assign varible to email field value
var email = $('form').find('input[type=email]').val();
// Extract the email's domain
var emailDomain = email.slice( ( email.indexOf('@') + 1 ), email.length );
// Test if the domain is in the blocked domains
($.inArray( emailDomain, blockedDomains ) > -1) ? e.preventDefault() : void(0);

});
});

Kind regards,

Candice

0 Upvotes
Kevin-C
Recognized Expert | Partner
Recognized Expert | Partner

Blocked Email domains on form submission

Hey @CandiceLloyd 

 

Would you mind sending me a link to the landing page and a list of the blocked domains? With this info I should be able to test thoroughly.

Kevin Cornett - Sr. Solutions Architect @ BridgeRev
0 Upvotes
CandiceLloyd
Participant

Blocked Email domains on form submission

0 Upvotes
Kevin-C
Recognized Expert | Partner
Recognized Expert | Partner

Blocked Email domains on form submission

Alright, so a few things:

Sorry I didn't catch this earlier .

It looks like HS handles form submissions differenctly than "native", so we've got to approach this differently.

 

Rather than catch the submission we'll have to prevent it all together.

 

The jQuery below fires a function when the email field changes. This function checks the email domain against the "blocked" list. If it is on the blocked list it disables the submit button.

$(document).ready(function(){
	// Get blocked array
	const blockedDomains = window.blockedDomains;
	// Assign varible to email field value
	var emailInput = $('form').find('input[type=email]');
	emailInput.change(function(e) {
		// Assign varible to email field value
		var email = $('form').find('input[type=email]').val();
		// Extract the email's domain
		var emailDomain = email.slice( ( email.indexOf('@') + 1 ), email.length );
		console.log('email changed');
		if ( $.inArray( emailDomain, blockedDomains ) > -1 ) {
			console.log('disabled the submit button');
			$('input[type="submit"]').css({'opacity':'0.5'}).prop("disabled", true);
		}
	});
});

I threw in a .css({'opacity':'0.5'}) so i could see it work. You can adjust or remove this if you don't want to give the user a hint as to why they can't submit the form.

Kevin Cornett - Sr. Solutions Architect @ BridgeRev
0 Upvotes
CandiceLloyd
Participant

Blocked Email domains on form submission

Thank you so much for all the extra effort you did for the testing.

I cleared out the contact list from the testing.

 

It seems that it still does not work on my side.

Once I add in a blocked email address the button is still clickable and I can submit my details too.

From there I am on our contact base list.

 

But thank you again for the help, I am going to spend some time this evening looking into it more.

0 Upvotes
CandiceLloyd
Participant

Blocked Email domains on form submission

Hey @Kevin-C 

 

I do apologize for being quiet, I still can't seem to get it to work.

I, however, did manage to disable the button but our contacts still go through to the contact base.

I spoke to a colleague yesterday to see if he could help and this is his feedback:

This is as far as I got - I catch the submit, but this is non-blocking, so I was hoping one could clear out the email value somehow.

 

<script>
window.addEventListener('message', event => {
if(event.data.type === 'hsFormCallback' && event.data.eventName === 'onFormSubmit') {
var email = event.data.data.find(item=>item.name=="email");

{% set all_domain_rows = hubdb_table_rows(2616498) %}
{% set all_domains = all_domain_rows|map(attribute='blockeddomains') %}
{% set sep = ',' %}

var get_domains = "{{ all_domains|join(sep) }}";
var all_domains = get_domains.split(',');

split_email = email.value.split('@');
domain = split_email[1];

if(all_domains.includes(domain)) {
console.log('Email not permitted.')
// can we change the email to blank or stop the value from going through?
};
}
});
</script>

 

 

So still fighting with it to see what can be done!

 

Kind regards,

Candice

0 Upvotes
Kevin-C
Recognized Expert | Partner
Recognized Expert | Partner

Blocked Email domains on form submission

Good morning @CandiceLloyd 

 

A lot happening here, which leaves room for error. Good news I think we can simplify this quite a lot! I'll try to squeeze some time in today to go through everything.

 

But at a glance:

The way you're populating the blocked domain variable with Hubl doesn't look correct. It looks like you're pushing objects into the array, which doesn't need to happen.

 

Also if you're using an attached JS file and a custom module to get the code on the page we may want to declare the blocked domain s array as a global variable on the window object.

window.domains = [
{% for row in hubdb_table_rows(*table#*) %}
  "'" ~ row.domain_column ~ "',"
{% endfor %}
];

With something like this we can access the data from anywhere on the page.

Kevin Cornett - Sr. Solutions Architect @ BridgeRev
0 Upvotes