Jump to content

Need to block user from submitting a registration form if their email domain does not contain certain words


Recommended Posts

I have looked and looked for a code that offers a contain and will display an error message.

My field name is EmailAddressd and it needs to contain the domain @123.com before they can submit. If they submit they need to display an error message saying something about their domain being incorrect and to contact their system administrator 

I found the below code on another form but this is not exactly what I need. Can anyone help? I am going to continue looking. thank you in advanced. 

 

Here is the code i found that does not work but it is very close in what I need

<script>
document.querySelector('#caspioform').onsubmit = function(e) {
    e.preventDefault();

var balance= document.getElementsByName('cbParamVirtual2')[0].value;


if(balance<0) 
 {
 alert('Not enough Balance!');
 }
else
{
 this.submit();
}

}
</script>

 

Link to comment
Share on other sites

Hello @nanglin,

Please try to paste this code to the Footer section of the Submission form. 

Disable HTML editor before pasting.

<script>
  document.addEventListener('BeforeFormSubmit', function(event) {

    let checkValue = document.querySelector("#InsertRecordfield_name").value; //replace the field_name with your local field name 
    
    if (!checkValue.includes('@123.com')) {
    	event.preventDefault();
      	alert("Your domain is incorrect, contact your system administrator");
    }
  
  })
</script>

Please replace the field_name and customize the alert message. 

Hope this solution helps you. 

Link to comment
Share on other sites

@CoopperBackpack

One more question... Always right lol 

Is there anyway to make it look in a table and if the data in that field does not match it then says the error message. So instead of a 

(!checkValue.includes('@123.com'))

It would check values in a table, in a field in that table?

My boss is trying to make this more scalable. 

 

Link to comment
Share on other sites

@nanglin,

Generally, it is possible to add a Virtual field to the DataPage, and set it to the Calculated value form element. You may use SQL syntax in the Calculated value field (so, you may select the values from the table and to compare them with the entered values ). Then you may use the received value to display the message. 

Here is the article regarding the Calculated Values  https://howto.caspio.com/datapages/datapage-components/calculated-values/

To suggest more accurately I need to better understand the expected result. Which values do you need to check? Could you provide me with an example? 

By the way, maybe this information will be helpful. You may restrict the submission of the data on the database level (sometimes this is required ). This can be done by the Triggered Action (if this feature is available in your plan). You need to delete from #inderted.

For example:

JzV9QNZ.png

Link to comment
Share on other sites

Hi,

@CoopperBackpack

We would like to keep an approved list of domains in a caspio datapage and if a user is not part of that domain (from the email they provide) they get that alert message

example scenario:

Caspio table

ID | DomainName 

id | @123.com

id | @113.com

Caspio data page

input script that looks at the caspio table field 'DomainName' and checks for items listed  in that field and if not listed in that table than it gives the error message. 

I hope that explains it more.

 

Link to comment
Share on other sites

@nanglin,

Thank you for your explanation. 

I can suggest a solution (maybe someone else will provide you with a more elegant one:) )

1. For example, the table has the following design:


erYc0l6.png

2. On the Submission Form you may add the Virtual field and set it to the Calculated Value.

vJDoiPv.png

This formula selects the domain in the email address that a user enters in the email field.

RIGHT([@field:email], Len([@field:email]) - CHARINDEX('@', '[@field:email]')+1) 

On the Advanced tab you may make the Calculated Value hidden (check the 'Hide Field' checkbox).

3.  Add the second Virtual field and set it to the Calculated Value as well.

The formula for the Virtual2:

CASE WHEN 
(SELECT DomainName FROM Domain_list WHERE DomainName LIKE '%[@cbParamVirtual1]%' and LEN(DomainName) = LEN('[@cbParamVirtual1]')) IS NULL 
THEN 0
ELSE 1
END

This formula searches for the pattern  and compare the length of the entered domain and domain in the table.

4. We need to modify the JS code in the Footer. Please note that this code works for the hidden Virtual2 field. 

<script>
  document.addEventListener('BeforeFormSubmit', function(event) {

    let checkValue = document.querySelector("[id*='cbParamVirtual2']").value;
    
    if (checkValue == '0') {
     event.preventDefault();
       alert("Your domain is incorrect, contact your system administrator");
    }
   else if (checkValue == '1') {
 	document.forms["caspioform"].submit();
}  
  })
</script>

Use your local names of the fields.

In case you need to check a few domains, then it should be better to use only JS by adding the additional conditions in the IF statement.  

Feel free to update this thread if you have any further questions. 

 

Link to comment
Share on other sites

Hi, @CoopperBackpack

This absolutely works but once the form is submitted, It submits the user, flashes the submit message I have added, Goes back to the form, then it gives an error message saying  Duplicate values are not allowed for one or more fields. 

I checked the other code and for some reason it is doing the same thing. This was working. 

I tried adding the original code into a new form and it says the same thing after submission. 

 

Link to comment
Share on other sites

Hello @nanglin,

I see. Since  there is the code to submit the form in the "else if" condition, once the Submit button is clicked, 2 records are submitted.

As I understand, the email field is unique in the table, so you see the message regarding the duplicates.

Please remove the else if condition and test the DataPage.

<script>
  document.addEventListener('BeforeFormSubmit', function(event) {

    let checkValue = document.querySelector("[id*='cbParamVirtual2']").value;
    
    if (checkValue == '0') {
     event.preventDefault();
       alert("Your domain is incorrect, contact your system administrator");
    }
  })
</script>

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...