Introduction:
In Dynamics 365, ensuring the integrity of data entered by users is critical for maintaining the accuracy and consistency of records. One common requirement is the validation of name fields, where inputs should only contain alphabetic characters and spaces. Failing to validate these fields can lead to inconsistent data, which may affect business processes downstream.
While Dynamics 365 provides built-in validations for many fields, sometimes specific requirements, like restricting certain characters in a name field, need custom solutions. In such cases, JavaScript web resources are a powerful tool for developers to implement custom logic.
In this blog, we’ll walk you through how to write a simple JavaScript function that validates a name field to ensure it only contains letters and spaces. This function will be integrated as a web resource in Dynamics 365 and triggered when users attempt to save a record.
By the end of this guide, you’ll understand how to:
- Create a custom name validation using JavaScript.
- Use Dynamics 365’s form context to retrieve and modify field values.
- Implement form save event handling to prevent submission when the validation fails.
Understanding Name Validation Requirements
Before we get into the code, let’s first clarify what we’re trying to achieve. When users input a name, we want to ensure that:
- The name consists only of alphabetic characters (A-Z, a-z).
- Spaces are allowed between words (for names with multiple parts like “John Doe”).
- No numbers, special characters, or punctuation marks are allowed.
To accomplish this, we’ll use JavaScript in combination with Dynamics 365’s form context to validate the name field before saving the form.
What is a JavaScript Web Resource in Dynamics 365?
In Dynamics 365, a JavaScript web resource is a custom script that can be referenced and used across forms, views, and other UI components. It allows you to extend the capabilities of your Dynamics instance by adding custom functionality, such as validations, calculations, or other business logic that is executed in the client-side browser.
For our case, the JavaScript web resource will:
- Retrieve the value from the name field.
- Apply a validation function using regular expressions to check if the name is valid.
- Alert the user and prevent form submission if the input does not meet the criteria.
JavaScript code
Now that we know what we need, let’s get into the code. First, we’ll write the validateName function that uses a regular expression to check if the name contains only letters and spaces.
function validateName(name) {
// Regular expression for letters and spaces only
const re = /^[a-zA-Z\s]+$/;
return re.test(name);
}
Breaking Down the Code:
- Regular Expression: The pattern
/^[a-zA-Z\s]+$/ensures that the string contains only letters (a-z,A-Z) and spaces (\s). - re.test(name): This method tests the string (name) against the pattern. It returns
trueif the name matches, andfalseotherwise.
This function will be our core validation logic. Next, let’s integrate this with Dynamics 365 to ensure that validation happens during the save operation.
To trigger the validation when a user attempts to save the form, we’ll use Dynamics 365’s executionContext and prevent the save operation if the name is invalid.
Here’s the complete code that includes both the validation and the logic to prevent form submission when the name is incorrect:
function validateName(name) {
// Regular expression for letters and spaces only
const re = /^[a-zA-Z\s]+$/;
return re.test(name);
}
function onSave(executionContext) {
const formContext = executionContext.getFormContext();
const nameField = formContext.getAttribute("abc_first_name"); // "abc_first_name" = the actual field name
const nameValue = nameField.getValue();
if (!validateName(nameValue)) {
const alertStrings = {
confirmButtonLabel: "OK",
text: "Please enter a name with only letters and spaces",
title: "Invalid Name Format"
};
const alertOptions = { height: 120, width: 260 };
Xrm.Navigation.openAlertDialog(alertStrings, alertOptions).then(
function success() {
// Prevent form submission if name is invalid
executionContext.getEventArgs().preventDefault();
},
function error() {
// Handle error if needed
}
);
// Prevent form submission if name is invalid
executionContext.getEventArgs().preventDefault();
return false;
}
return true; // Allow form submission if name is valid
}
Explanation of the Code
- Form Context: We access the form’s context using
executionContext.getFormContext()to interact with the fields on the form. - Retrieving Field Value: The name field is accessed using its schema name (
abc_first_name), and the value is retrieved vianameField.getValue(). - Validation: The
validateNamefunction checks the name. If the value fails the validation (i.e., it contains numbers or special characters), the form save is prevented. - User Alert: If validation fails, the user is notified through an alert dialog using
Xrm.Navigation.openAlertDialog(). This lets them know that they need to correct the input before the form can be saved.
Deploying the JavaScript Web Resource in Dynamics 365
Now that we have the JavaScript code, the next step is to deploy it as a web resource in Dynamics 365. Here’s how to do it:
Create the Web Resource:
Go to your solution -> New -> more -> Web Resource

Create a new web resource. Upload your JavaScript file here.

Attach the Web Resource to the Form:
Go to Table where you want to add validation -> Forms -> select Form. In Edit Form window go to ‘Form Properties’ as shown below.

In Event tab (in form properties), select ‘+ Event Handler’ in ‘On Save’, click ‘+ Add library’ seach and select your web resource. Add the same function name as in the js web resource (in my case we use ‘onSave’ as you can see in the code).
Remember to select ‘Enabled’ and ‘Pass execution content as first parameter’.
Save and Publish the form.

Publish the Changes:
After adding the JavaScript web resource and configuring the form, make sure to publish your customizations.
Now let’s test, if we don’t put alphabet or space in the name field and try to save we should get this below notification.

Real-World Use Cases for Name Validation
While the validation of names may seem simple, it plays a critical role in ensuring data quality, especially in large-scale systems like Dynamics 365. Here are some real-world scenarios where this validation can be particularly useful:
Customer Relationship Management (CRM) Systems:
In CRM systems, having clean and standardized data is essential for effective communication and analytics. Ensuring that customer names do not contain numbers or special characters helps maintain a professional and consistent appearance across customer records, emails, and reports.
Business Process Automation:
If your business processes include automatic generation of customer-facing documents (such as contracts or emails), ensuring valid names can prevent embarrassing mistakes like “John Doe#12” appearing in communications or legal documents.
Data Migration and Import Processes:
When migrating data into Dynamics 365 from legacy systems, it’s not uncommon to encounter inconsistent name formats. Having a name validation function in place can act as a safeguard during data import, catching invalid entries before they are saved into the system.
Online Forms and Portals:
If you’re using Dynamics 365 in conjunction with customer-facing forms or portals (like Power Apps portals), name validation helps prevent spam or incorrect data being submitted by users who may accidentally or intentionally enter invalid characters.
Conclusion:
By adding this simple name validation, you’re taking an important step in maintaining the integrity of your data in Dynamics 365. Custom JavaScript web resources allow you to go beyond the out-of-the-box capabilities of the platform, giving you the power to enforce business rules and improve user experience.
Whether you’re validating customer names, product codes, or any other type of field, this approach can be easily adapted to meet your needs. As data quality becomes increasingly important in today’s business landscape, solutions like these will help ensure that your system is always populated with clean, standardized data.
Thanks for reading till the very end, feel free to ask any questions or queries. We will be more than happy to answer them.

Leave a comment