web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Validate phone number JS

(0) ShareShare
ReportReport
Posted on by

Hello expert ,

I need to validate a phone number that contain up to 10 digits without spaces & should not start  with 04 or 05.

this is my function:

function ValidatePhoneNumber() {
debugger;
var mobilenumber = Xrm.Page.getAttribute("telephone1").getValue();
if (mobilenumber != null && mobilenumber != undefined) {
var pattern = /^[0-9]\d{2,4}\d{6,8}$/;
if (mobilenumber.match(pattern)) {
return true;
}
else {

alert("Please Enter Valid Mobile NUmber");
Xrm.Page.getAttribute("telephone1").setValue(null);
}

}

}

Thank you in advance

*This post is locked for comments

I have the same question (0)
  • Verified answer
    Community Member Profile Picture
    on at

    Hi David Thorn,

    I am guessing that your question is that it is not working. The problem seems to be because of the regular expression.

    Can you please try this pattern?

    ^(?!(04|05))(?=\d{10}$)\d+/

    This is the pattern for checking if the first two digits are not 04 or 05, not containing any spaces and is exactly 10 digits long. You can easily verify the regex by running it in Chrome Developer Tools. A demo is available at https://regex101.com/r/E9NFF2/1/

    - Sara

  • Community Member Profile Picture
    on at

    Hello Sara ,

    Thank you for your answer.

    But i got an error when i sue this pattern , my function is not defined at eval time ,when i get back to my script its wworking fine but the validation not.

    Thank you

  • Community Member Profile Picture
    on at

    I just find why ,a bracket is missing  /^(?!(04|05))(?=\d{10}$)\d+/

    Thank you so much for your help & for the link!

  • Community Member Profile Picture
    on at

    and if i want a validation of a number that starts with : 04 or 05

    i tried /^(?:04|05)(?=\d{10}$)\d+/  but its not working

  • Community Member Profile Picture
    on at

    I just find It!thanks

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans