Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Answered

How can i create a error message through label using javascript in CRM

(1) ShareShare
ReportReport
Posted on by 45

i was create phone number validation on quick create account in CRM

how can i display a  error message through label using javascript in CRM

Can you please help any one. 

My Code: 
----------

function Phonevalidation() {

var phoneCleaned;

var phoneNumber = Xrm.Page.getAttribute("telephone1");

if (phoneNumber.getValue() != null) {

phoneCleaned = phoneNumber.getValue().replace(/[^0-9]/g, "");

if (phoneCleaned.length !== 10) {

alert(" must contain 10 numbers.");

Xrm.Page.getAttribute("telephone1").setValue(null);

return false;
}
phoneNumber.setValue("(" + phoneCleaned.substr(0, 3) + ") " + phoneCleaned.substr(3, 3) + "-" + phoneCleaned.substr(6, 4));
}
return true;
}



Thank you

  • Sri College Profile Picture
    Sri College 45 on at
    RE: How can i create a error message through label using javascript in CRM

    Thank you Clofly

    sorry for late reply.

    I will check your code then update to you.

    I am using this below line for mobile number do not begin with zero 

    var regex = /^(?:[1-9][0-9]*)$/;

  • cloflyMao Profile Picture
    cloflyMao 25,202 on at
    RE: How can i create a error message through label using javascript in CRM

    Hi Sree,

    Please let me know whether the solution could work for you. :)

  • cloflyMao Profile Picture
    cloflyMao 25,202 on at
    RE: How can i create a error message through label using javascript in CRM

    Hi Sree,

    Try following code:

    var pattern1 = /^0/;
    
    var pattern2 = /^1/;
    
    var phoneNumber = Xrm.Page.getAttribute("telephone1").getValue();
    
    var found = pattern1.test(phoneNumber) || pattern2.test(phoneNumber);
    
    console.log(found);

    If the phone number not begins with 0 or 1, then the found variable will be false.

    pastedimage1615359862505v1.png

    Reference:

    https://stackoverflow.com/questions/26068100/validate-u-s-phone-numbers-only

  • Sri College Profile Picture
    Sri College 45 on at
    RE: How can i create a error message through label using javascript in CRM

    Thank you for your answer Clofly ......

    I have one more question

    how to do a validation for us phone number not to begin with 0 or 1?

    Can you please help me

  • Verified answer
    cloflyMao Profile Picture
    cloflyMao 25,202 on at
    RE: How can i create a error message through label using javascript in CRM

    Hi Sree,

    Try setNotification method.

    Xrm.Page.getControl("telephone1").setNotification("Please type phone number in correct format");

    However, the notification will render as tooltip in classic web client.

    pastedimage1615281715999v1.png

    While in UCI, it is changed to label with error message.

    pastedimage1615281998803v2.png

    https://docs.microsoft.com/en-us/powerapps/developer/model-driven-apps/clientapi/reference/controls/setnotification

    https://neilparkhurst.com/2015/11/02/javascript-notifications/

Under review

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

Helpful resources

Quick Links

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Tip: Become a User Group leader!

Join the ranks of valued community UG leaders

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,401 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans