Sunday, 21 October 2018

Validation are allowed in MSCRM using JavaScript

1. The contacts first and last name must be alphabets ([A-Z][a-z])
function validateLastName(executionContext) {
   var formContext = typeof executionContext != 'undefined' ? executionContext.getFormContext() : Xrm.Page; // get formContext
   
   var pattern = /[^a-z]/ig;
   var fieldName = 'lastname';
   var currentValue = formContext.getAttribute('lastname').getValue(); 
   if (pattern.test(currentValue)) {
     formContext.getControl(fieldName).setNotification('Invalid value, please only enter letters.');
   } else {
     formContext.getControl(fieldName).clearNotification();
   }

}
2. The account names can have 8 special characters such as & , ', ",  #, -, /, <, >
function validateAccountName(executionContext) {
   var formContext = typeof executionContext != 'undefined' ? executionContext.getFormContext() : Xrm.Page; // get formContext
   
   var pattern = /[^a-z0-9&\'"#\-\/<>]/ig;
   var fieldName = 'name';
   var currentValue = formContext.getAttribute(fieldName).getValue(); 
   if (pattern.test(currentValue)) {
     formContext.getControl(fieldName).setNotification('Invalid value, only these special characters are allowed: &\'" #-/<>');
   } else {
     formContext.getControl(fieldName).clearNotification();
   }

}
3. The city name must be alphabets ([A-Z][a-z])
function validateCity1(executionContext) {
   var formContext = typeof executionContext != 'undefined' ? executionContext.getFormContext() : Xrm.Page; // get formContext
   
   var pattern = /[^a-z]/ig;
   var fieldName = 'address1_city';
   var currentValue = formContext.getAttribute(fieldName).getValue(); 
   if (pattern.test(currentValue)) {
     formContext.getControl(fieldName).setNotification('Invalid value, please enter only letters.');
   } else {
     formContext.getControl(fieldName).clearNotification();
   }

}
4. The phone number attribute can have only  0-9, -, (,) characters as a valid characters
function validateTelephone1(executionContext) {
   var formContext = executionContext ? executionContext.getFormContext() : Xrm.Page; // get formContext
   
   var pattern = /[^0-9,\-]/ig;
   var fieldName = 'telephone1';
   var currentValue = formContext.getAttribute(fieldName).getValue(); 
   if (pattern.test(currentValue)) {
     formContext.getControl(fieldName).setNotification('Invalid value, please enter only numbers or - and ,');
   } else {
     formContext.getControl(fieldName).clearNotification();
   }
}
5. The postal code attribute can have only 0-9, - characters as a valid characters
function validatePostalCode(executionContext) {
   var formContext = typeof executionContext != 'undefined' ? executionContext.getFormContext() : Xrm.Page; // get formContext
   var pattern = /[^0-9\-]/ig;
   var fieldName = 'address1_postalcode';
   var currentValue = formContext.getAttribute(fieldName).getValue(); 
   if (pattern.test(currentValue)) {
     formContext.getControl(fieldName).setNotification('Invalid value, please enter only numbers or -');
   } else {
     formContext.getControl(fieldName).clearNotification();
   }
}

No comments:

Post a Comment