Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Dynamics 365 general forum

Make a field accept only numbers

(0) ShareShare
ReportReport
Posted on by 55

Hi,

I have a phone field that is set to 11 digits but accepts any characters entered. How can I set the field to only accept digits and not text or special characters?Mobile-Phone-field.jpg

  • gdas Profile Picture
    50,091 Moderator on at
    RE: Make a field accept only numbers

    Hi,

    The Javascript which I have shared already tested , you can go with this without any problem, and don't forget to verify the answers.

  • JustLearning Profile Picture
    55 on at
    RE: Make a field accept only numbers

    Thank you all for your responses. I'll hopefully find out if the javascript has done the job in the near future.

  • Verified answer
    LuHao Profile Picture
    40,886 on at
    RE: Make a field accept only numbers

    Hi JustLearning,

    I agree with Ravi that the most appropriate method is to use JavaScript and regular expression to limit the text.

    If you recreate the Mobile Phone field as a whole number field, it is not suitable for 11 digits.

    Because the maximum value of whole number field is 2,147,483,647, does not match the range of 11 digit phone numbers.(0~99,999,999,999)

    2018.1.JPG

    And if the mobile phone starts with a few 0, the first few 0 will be deleted. E.g. 05101234567->5101234567

    So the best way is to use JavaScript and regular expression.

    Hope this helps.

    Best Regards,

    Lu Hao

  • Verified answer
    RaviKashyap Profile Picture
    55,410 Moderator on at
    RE: Make a field accept only numbers

    Hi,

    As suggested by Goutam above, you will need to use JS to restrict the number. The code share should work for you. You can refer the below article if you are not familiar with JS or how to use.

    neilparkhurst.com/.../javascript-basics

    Hope this helps.

  • Suggested answer
    Adrian Begovich Profile Picture
    1,025 Super User 2025 Season 1 on at
    RE: Make a field accept only numbers

    Hi JustLearning,

    If you recreate the Mobile Phone field as a whole number field then the field will only accept integer values (Numbers).

  • Verified answer
    gdas Profile Picture
    50,091 Moderator on at
    RE: Make a field accept only numbers

    Hi ,

    Try with this  -

    // Add below functions in your JS web resource 
    // Register only onLoadEvents function in the form onload
    function onLoadEvents()
    {
        Xrm.Page.getControl('mobilephone').addOnKeyPress(AllowOnlyNumbers);
    }
    
    function AllowOnlyNumbers() {      
        var phoneNo = Xrm.Page.getControl("mobilephone").getValue();
        phoneNo = phoneNo.replace(/\D/g, '');
        Xrm.Page.getAttribute('mobilephone').setValue(phoneNo);
        if (phoneNo.length == 11) {
            var formattedPhone = phoneNo.replace(/(\d{3})(\d{3})(\d{4})/, '$1-$2-$3');
            Xrm.Page.getAttribute('mobilephone').setValue(formattedPhone);
        }
    }
    

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

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard > Dynamics 365 general forum

Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans