Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)
Under review by Community Managers

Under review

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

Contact Form JavaScript for validation

Posted on by

Please help me this: I have attached what I did so far

how to change a label on one of the fields.

how to Make some fields mandatory.

how to do some error checking on one of your fields.

how to Set & clear a notification on your form depending on whether there is a validation violation.

Change Spouse/Partner Name label in the Details section to Next of Kin.

Make the following fields mandatory: Job Title and Personal Notes

Do the following validation: Job title should be alphabetical, spaces - and _ only

function validatePreferredMethodOfCommunication() {
   //get the value of Preffered Method of Communication code
   var preferredcontactmethodcode =
      Xrm.Page.getAttribute('preferredcontactmethodcode').getValue();  
   
   //if Preferred Method = Any, make all fields as non-mandatory
   
   //else if Preferred Method = Personal Notes, make Personal Notes field mandatory
   //and all other fields as non-mandatory
   
   //else if Preferred Method = Job Title, make Job Title field mandatory
   //and all other fields as non-mandatory
   
   
   if(preferredcontactmethodcode == 1) {
      clearAllMandatoryFields();
   }  
   if(preferredcontactmethodcode == 2) {
      clearAllMandatoryFields();
      Xrm.Page.getAttribute('PersonalNotes').setRequiredLevel('required');
   } else if(preferredcontactmethodcode == 3) {
      clearAllMandatoryFields();
      Xrm.Page.getAttribute('JobTitle').setRequiredLevel('required');
   }
}
function clearAllMandatoryFields() {
   
   //clear all mandatory fields
   Xrm.Page.getAttribute('PersonalNotes').setRequiredLevel('none');
   Xrm.Page.getAttribute('JobTitle').setRequiredLevel('none');
function validateName() {
    var field = Xrm.Page.getAttribute('Jobtitle');
    var val = field.getValue();
    var passed = isAllLetters(val);
    var formContext = getFormContext();
    if (passed) {
        if (val.length <= 30) {
            formContext.getControl('Jobtitle').clearNotification();
            return true;
        } else {
            formContext.getControl('Jobtitle').setNotification('This field must be 30 characters or less!');
            return false;
        }
    } else {
        formContext.getControl('Jobtitle').setNotification('This field must only contain alphabetical values!');
        field.setValue(""); // This is to clear the field and is not needed in our case
        return false;
    }
}
function getFormContext() {
     return typeof executionContext != 'undefined' ? executionContext.getFormContext() : Xrm.Page; // get formContext
}
function isAllLetters(inputtxt)
{
    // Regex for letters, numbers, spaces, comma, question mark, exclamation mark and period => ^[\.a-zA-Z0-9,!? ]*$
    if(!/^[a-zA-Z]*$/g.test(inputtxt)) // no spaces allowed, only letters.
    {
      // alert('This field must only contain alphabetical values');
      return false;
    }
    else
    {
      return true;
    }
}
function setMandatoryFields() {
    Xrm.Page.getAttribute('parentcustomerid').setRequiredLevel('required');
    Xrm.Page.getAttribute('Job Title').setRequiredLevel('required');
   
}​​​​​​​

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans