locked
Validation RRS feed

  • Question

  • Hello,

    Can someone help me create a code for a field in Dynamics 365 form with this conditions:

    1. you must enter 15 digits into the field.

    2. if the field is empty then it's okay.

    3. if the field is contain more or less than 15 than show error message : "You must enter 15 digits".

    Thanks & Best Regards,

    Hauw Indra

    Wednesday, May 3, 2017 4:56 AM

Answers

All replies

  • Hello,

    Can someone help me create a code for a field in Dynamics 365 form with this conditions:

    1. you must enter 15 digits into the field.

    2. if the field is empty then it's okay.

    3. if the field is contain more or less than 15 than show error message : "You must enter 15 digits".

    Thanks & Best Regards,

    Hauw Indra

    Wednesday, May 3, 2017 9:03 AM
  • Hey,

    Here are my advices:

    1. How to get value from field - var fieldValue = Xrm.Page.getAttribute("field schema name").getValue();

    2. How to check length - var fieldLength = fieldValue.length;

    3. How to validate that value contains only digits - http://stackoverflow.com/questions/1779013/check-if-string-contains-only-digits

    4. How to notify user about invalid data - https://msdn.microsoft.com/en-us/library/gg334266.aspx#BKMK_setnotification or https://msdn.microsoft.com/en-us/library/jj602956.aspx#BKMK_alertDialog


    Dynamics CRM MVP
    Read My blog
    Subscribe for one of my courses

    Wednesday, May 3, 2017 4:31 PM
    Moderator
  • You can white plugin, witch validate your field.
    https://msdn.microsoft.com/en-us/library/gg328490.aspx

    Create a plugin, put it on pre-create and pre-update(In update step, better choose only needed attributes in a filter attributes. In your case you can choose only "your_field") your entity action.

    In plugin check your conditions and throw exceptions, when something wrong

    For example:

    if(entity.contains("your_field") &&
    entity.GetAttributeValue<string>("your_field").length > 0 &&
    entity.GetAttributeValue<string>("your_field").length != 15)
    {
    throw new Exception("your_field length must be 15 symbols!");
    } 



    • Edited by xjomanx Thursday, May 4, 2017 6:19 AM
    Thursday, May 4, 2017 6:14 AM
  • Look here:

    https://social.msdn.microsoft.com/Forums/en-US/441b3ab9-6514-4a39-9c63-310677508c98/code?forum=crmdevelopment

    Thursday, May 4, 2017 6:21 AM
  • try this code onchange event. Also try it onsave event:-

    function TemsAndCondtitionsValidation(context) {
        var termsConds = Xrm.Page.getAttribute(ATTRIBUTESCHEMANAME).getValue();
        if (termsConds.length == 0 || termsConds == null) {
            //Do nothing
        }
        else if (termsConds.length < 15 || termsConds.length > 15) {
            Xrm.Utility.alertDialog("You must enter 15 digits");
            context.getEventArgs().preventDefault(); // cancel save
        }
    }


    Regards Faisal

    Friday, May 5, 2017 9:59 AM