Quantcast
Channel: Microsoft Dynamics CRM
Viewing all articles
Browse latest Browse all 154803

Forum Post: RE: Field validation using Javascript

$
0
0
Hey Lucas, Using "alert" in your client script is not supported and will break your forms in the future. For field validation in particular it is recommended that you use the "setNotification" method on controls. This adds a red X Docs: https://docs.microsoft.com/en-us/dynamics365/customer-engagement/developer/clientapi/reference/controls/setnotification I had to take some liberties in assuming which fields you need exactly but, they can easily be adjusted. 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(); } } The "new RegExp" constructor in your script is also not necessary, just starting a string variable assignment with / will trigger javascript to assume a regular expression. The variable formContext you see at the start of every line is taking into account the deprecation for Xrm.Page in version 9. From version 9 onward getFormContext should be used, instead of Xrm.Page - this of course only works if you pass the execution context on your calling of the events. Also a quick tip: To validate/test regular expressions I always use https://regex101.com/ -- If you have any other questions or are having issues with these scripts, please let me know. If you found my answer helpful, please mark it as such/verified :-)

Viewing all articles
Browse latest Browse all 154803

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>