Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Field validation using Javascript

(0) ShareShare
ReportReport
Posted on by Microsoft Employee

Hello,

The CRM environment of my organization send a XML file to the Department of State almost everyday with several of records, the DOS has a few requirements for fields and there are characters that are not allowed. Initially I was using this code to validate these fields

function ValidateSpecialChar()

{

var Regexp=/[^a-zA-Z0-9 ]/;  //this will allow space

var value=Xrm.Page.getAttribute("name").getValue();

if (Regexp.test(value)) {

 alert("Special Character is Not Allowed !!");

 Xrm.Page.getAttribute("name").setValue(null);

 }

else {

   return false;

 }

}

 

However there are fields that do allow some characters:

  1. The contacts first and last name must be alphabets ([A-Z][a-z])
  2. The account names can have 8 special characters such as & , ', ",  #, -, /, <, >
  3. The city name must be alphabets ([A-Z][a-z])
  4. The phone number attribute can have only  0-9, -, (,) characters as a valid characters
  5. The postal code attribute can have only 0-9, - characters as a valid characters

 Would anyone be able to help me achieve this with Javascript? I can compensate you for time and effort.

*This post is locked for comments

  • Verified answer
    Michel van den Brink Profile Picture
    Michel van den Brink 4,697 on at
    RE: Field validation using Javascript

    Hey Lucas,

    Glad to hear things are coming together :-)

    The dot is a special character in regular expressions, so to match it exactly (instead of applying it's special meaning) you add a backslash in front of it.

    For example, this first line doesn't match the dot, but the second one does:

       var pattern = /[^a-z0-9&\'"#\-\/<>]/ig;
       var pattern = /[^a-z0-9&\'"#\-\/<>\.]/ig;


  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Field validation using Javascript

    Hi Michel,

    I will investigate this as I have troubleshoot it as well and everything looks correct.

    But in the meantime, how do I include (.) in the validation?

  • Michel van den Brink Profile Picture
    Michel van den Brink 4,697 on at
    RE: Field validation using Javascript

    Hey Lucas,

    The script for account name not allowing you to enter anything is a little strange. I just tried it in a test organization and the script works well.
    Perhaps it's a copy-paste issue... there's some special characters that might have gone loopy due to the way the Dynamics Community renders them, or the way they are being copied. Here's a neat snippet of the script for account name: https://jsfiddle.net/9da8fba1/1/embedded/js/

    The code for last name, please make sure you replaced the formContext variable with Xrm.Page in all places, just like the script for the account name.
    The composite field for fullname shouldn't impact this, it supports the same events for it's containing fields (firstname and lastname) as long as you register them on the OnChange events for the respective fields, not on the event for the composite itself.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Field validation using Javascript

    Thank you Michel for sharing the code for 2016 on-premises 8.1

    I have tried your code for the Account Name and now I don't get the script error, but it's not allowing me to input any special character. See below:

    Validation1.jpg

    This is the code I am using:

    function validateAccountName(executionContext) {
    var pattern = /[^a-z0-9&\'"#\-\/<>]/ig;
    var fieldName = 'name';
    var currentValue = Xrm.Page.getAttribute(fieldName).getValue();
    if (pattern.test(currentValue)) {
    Xrm.Page.getControl(fieldName).setNotification('Invalid value, only these special characters are allowed: &\'" #-/<>');
    } else {
    Xrm.Page.getControl(fieldName).clearNotification();
    }
    }

    When I tried your code for first and last name, I still get the error. But I wonder if I need to have first and last name on the form, note that I am using the Full name composite. Additionally, should I add anything on this section?

    Validation1.jpg

  • Suggested answer
    Michel van den Brink Profile Picture
    Michel van den Brink 4,697 on at
    RE: Field validation using Javascript

    Hey Lucas,

    Looks like my method to grab to formContext doesn't work quite right on CRM 2016 / 8.2, my apologies, I didn't quite test it for that, I started with a script that assumes version 9.

    You can remove this line

    var formContext = typeof executionContext != 'undefined' ? executionContext.getFormContext() : Xrm.Page; // get formContext


    And everywhere you see a reference to formContext, replace that with Xrm.Page

    Here's what the updated script would look like for account name:

    function validateAccountName(executionContext) {
       var pattern = /[^a-z0-9&\'"#\-\/<>]/ig;
       var fieldName = 'name';
       var currentValue = Xrm.Page.getAttribute(fieldName).getValue(); 
       if (pattern.test(currentValue)) {
         Xrm.Page.getControl(fieldName).setNotification('Invalid value, only these special characters are allowed: &\'" #-/<>');
       } else {
         Xrm.Page.getControl(fieldName).clearNotification();
       }
    }



  • Aric Levin Profile Picture
    Aric Levin 30,188 on at
    RE: Field validation using Javascript

    Yes, just noticed.

    Wei is right.

    If you are on CRM 2016 you need to use:

    Xrm.Page.getControl(fieldName).setNotification('Invalid value, please only enter letters.');

    Good catch

  • Suggested answer
    Alex Fun Wei Jie Profile Picture
    Alex Fun Wei Jie 33,626 on at
    RE: Field validation using Javascript

    Hi,

    you should use

    Xrm.Page.getControl(fieldName).setNotification('Invalid value, please only enter letters.');

    Xrm.Page.getControl(fieldName).clearNotification();

    msdn.microsoft.com/.../gg334266.aspx

    The method that you use only working in CRM online and V9.0, but you are using CRM 2016

    docs.microsoft.com/.../setnotification

    remove all the code related to executioncontext and use back the Xrm method

  • Aric Levin Profile Picture
    Aric Levin 30,188 on at
    RE: Field validation using Javascript

    Can you please check the value of formContext before the var pattern line?

    Xrm.Page.getControl(fieldName).setNotification(message) and

    formContext.getControl(fieldName).setNotification(message) are correct syntax for this.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Field validation using Javascript

    I have modified the code to use with First Name and Last Name field. This is how the code is:

    function validateFirstName(executionContext) {
    var formContext = typeof executionContext != 'undefined' ? executionContext.getFormContext() : Xrm.Page; // get formContext

    var pattern = /[^a-z]/ig;
    var fieldName = 'firstname';
    var currentValue = formContext.getAttribute(fieldName).getValue();
    if (pattern.test(currentValue)) {
    formContext.getControl(fieldName).setNotification('Invalid value, please only enter letters.');
    } else {
    formContext.getControl(fieldName).clearNotification();
    }

    }

    And the screenshots show how it has been setup and the script error:

    Validation.jpg

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Field validation using Javascript

    I am actually using the Full Name composite, would the validation work with that?

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

Congratulations 2024 Spotlight Honorees

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December

Congratulations to our December super stars! 🥳

Start Your Super User Journey

Join the ranks of our community heros! 🦹

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,711 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,458 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans