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 :
Customer experience | Sales, Customer Insights,...
Answered

How to restrict the custom text field to accept only numbers with minimum 9 digits and maximum 14 digits

(0) ShareShare
ReportReport
Posted on by 342

How to restrict the  text field to accept only numbers with minimum 9 digits and maximum 14 digits. How can i achieve this with Java script code

I have the same question (0)
  • Suggested answer
    LeoAlt Profile Picture
    16,331 Moderator on at

    Hi Partner,

    Please refer to the following code.

    function checkNumberDigits(executionContext){
        var formContext=executionContext.getFormContext();
        var textValue=formContext.getAttribute("fieldNaame").getValue();
        //check if the text value is number type
        if (!isNaN(textValue)) {
            var length=textValue.length;
            if(length>14||length

    Add this code in your field onChange event.

    Regards,

    Leo

  • Sandy Hello Profile Picture
    342 on at

    Hi Leo thanks for your response, the code is working well but accepting alphabets too

  • Verified answer
    Bipin D365 Profile Picture
    28,983 Moderator on at

    HI,

    function checkNumberDigits(executionContext) {
        var formContext = executionContext.getFormContext();
        var textValue = formContext.getAttribute("fieldNaame").getValue();
        //check if the text value is number type
        if (!isNaN(textValue)) {
            var length = textValue.length;
            if (length > 14 || length < 9) {
                alert("Number should with minimum 9 digits and maximum 14 digits!")
            }
        }
        var patt = new RegExp("^[0-9]*$");
        if (!patt.test(textValue)) {
            alert("Only Numbers/Digits between 0-9 allowed");
        }
    }

    You should add this code on save event to prevent user to save the record. If you go with onchange event then only popup will be displayed but user will be able to save the record.

    docs.microsoft.com/.../preventdefault

    Please mark my answer verified if i were helpful

  • Sandy Hello Profile Picture
    342 on at

    Hi Bipin, thanks for your response the above code works well for number of digits and accept only numbers, this is perfect for Onchange event.

  • LeoAlt Profile Picture
    16,331 Moderator on at

    Hi Partner,

    Did you try my solution in your instance?

    If my answer also helped you, please mark my answer as verified.

    Regards,

    Leo

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 > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 170 Super User 2025 Season 2

#2
#ManoVerse Profile Picture

#ManoVerse 70

#3
Jimmy Passeti Profile Picture

Jimmy Passeti 50 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans