Skip to main content

Notifications

Dynamics 365 general forum
Answered

Make a field accept only numbers

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
    gdas 50,083 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
    JustLearning 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
    LuHao 40,858 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
    RaviKashyap 55,406 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
    Adrian Begovich 20,989 Super User 2024 Season 2 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
    gdas 50,083 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);
        }
    }
    

Helpful resources

Quick Links

Dynamics 365 Community Update

Welcome to the inaugural Community Platform Update. As part of our commitment to…

Dynamics 365 Community Newsletter - August 2024

Catch up on the latest D365 Community news

Community Spotlight of the Month

Kudos to Mohana Yadav!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,142 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 227,979 Super User 2024 Season 2

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans