web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Validate/restrict email address domain in Contact record via js

(0) ShareShare
ReportReport
Posted on by 12,163

I want to restrict users from using our internal email domain as an email address in a Contact. This is so internal emails do not get tracked in the system as I have found no other efficient way of preventing this. I have come across a script online that I am trying to make work as an onsave event but am wondering if this has been tackled already in this forum.

*This post is locked for comments

I have the same question (0)
  • Joe Woltering Profile Picture
    12,163 on at
    RE: Validate/restrict email address domain in Contact record via js

    Ah, yes. I see now. Got it. Thanks again!

  • jlattimer Profile Picture
    24,562 on at
    RE: Validate/restrict email address domain in Contact record via js

    The updated script would look like:

    function Email_OnChange() {
        if (Xrm.Page.getAttribute("emailaddress1").getValue() != null) {
            if (Xrm.Page.getAttribute("emailaddress1").getValue().indexOf("@domain.com") !== -1) {
                alert("Invalid email");
                Xrm.Page.getAttribute("emailaddress1").setValue(null);
            }
        }
    }
    
    What I threw together first would have given an error if the script tried to run when the field value was empty - if you had a value and then deleted it. The ".indexOf" method assumes a value is present, if it would run against a null value there would be an error.
  • Joe Woltering Profile Picture
    12,163 on at
    RE: Validate/restrict email address domain in Contact record via js

    Guido,

    Could you clarify what this means? better to check if the field has value or it will throw an error if you do indexOf of an empty field. Not quite sure I understand.

    I can't imagine someone typing domain.com@domain.com but sure, good to have it in there.

    Thanks!

  • jlattimer Profile Picture
    24,562 on at
    RE: Validate/restrict email address domain in Contact record via js

    Good catch!

  • Joe Woltering Profile Picture
    12,163 on at
    RE: Validate/restrict email address domain in Contact record via js

    Brilliant! Thanks very much. Works perfectly!

  • Verified answer
    jlattimer Profile Picture
    24,562 on at
    RE: Validate/restrict email address domain in Contact record via js

    You could attach this script to the field's OnChange event to check:

    function Email_OnChange() {
        if (Xrm.Page.getAttribute("emailaddress1").getValue().indexOf("domain.com") !== -1) {
            alert("Invalid email");
            Xrm.Page.getAttribute("emailaddress1").setValue(null);
        }
    }
    

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Mansi Soni – Community Spotlight

We are honored to recognize Mansi Soni as our August 2025 Community…

Congratulations to the July Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
Community Member Profile Picture

Community Member 2

#2
Christoph Pock Profile Picture

Christoph Pock 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans