Task -6 JavaScript , Regular expressions

User Story: - 

On Change of phone number, need to validate, it should contain only numbers and + symbol if any other characters are there then show the error "Invalid value, please enter only numbers or +"

Sudo Code: - 

1. Create a javascript function with executionContext as a parameter.

2. Retrieve the phone number field value.

3. Create a pattern to check the value contains numbers and + only by using javascript regular expressions.

4. Using "test" string method check the requirement, it returns true or false.

5. If the return value is false then set Notification to the phone number field using setNotification control method.

6. If the return value is true then clear Notification to the phone number field using clearNotification control method.

7. Create a web resource using this javascript code and publish it.

8. Create an OnChange event to the phone number field using this web resource and function.



Show Error message if mobile number field contains characters other than + or numbers in Dynamics 365 using JavaScript

  • Open an entity from a solution in the Dynamics 365 CRM.
  • Create a single line of text field and then place it in the form and publish it.
  • Create a JavaScript Web Resource by using the below JavaScript code.

function ValidatePhoneNumber(executionContext) {

    var formContext = executionContext.getFormContext();

    var number = formContext.getAttribute("naree_phoneinbuilt").getValue();


    //Please learn the Regular expressions to create a pattern


    const pattern = /^\+?\d{10,}$/;

    let result = pattern.test(number);

    var NotificationId = "1023";

    if (result)

    {

        formContext.getControl("naree_phoneinbuilt").clearNotification(NotificationId);

    }

    else

    {

        formContext.getControl("naree_phoneinbuilt").setNotification("Invalid value please enter only numbers or +", NotificationId)

    }

}



  • Use this web resource to create an OnChange Event with the given function.



If you enter characters other than + or numbers it will show an error message.






Comments

Popular posts from this blog

How to create an Azure AD(Active Directory) and create an application to connect Dynamics 365 from console application

Task_1 JavaScript