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

Community site session details

Session Id :
Small and medium business | Business Central, N...
Suggested answer

How to validate Phone No field

(0) ShareShare
ReportReport
Posted on by 120

Hi All,

I have a requirement to add extra Phone no field on Customer card. I want to display error if a letter is entered as in default Phone no field.can you help me with writing custom validation code?

(when trying to copy same validation code from within the validation trigger in default Phone field , it give error message.'')

PhoneNo.png

Thanks,

Nalin.

I have the same question (0)
  • Lars Lohndorf-Larsen Profile Picture
    on at
    RE: How to validate Phone No field

    Hello,

    I see this is odd, since the same code works OK in the standard app. But for extensions you are not allowed to make use of local external resources unless you set the project to be for OnPrem. You can do that in App.json by setting target = onprem:

    pastedimage1624008805985v1.png

    Or you can maybe do the validation without the need for a .net variable like this:

                        if StrLen(rec."Phone No."<> 0 then
                            if not evaluate(i, rec."Phone No.") then
                                Error(StrSubstNo('Invalid %1', FieldCaption(rec."Phone No.")));
        var
            i: Integer;

    I have not tested this code, so no guarantees offered :-).

  • Nalin.karu Profile Picture
    120 on at
    RE: How to validate Phone No field

    Hi Lars,

    Thanks for the reply.

    sorry for not mentioning platform for which I'm developing. It's Cloud platform. so, Is it safe to set "target":OnPerm".?.

    Appreciate your explanation.

    Regards,

    Nalin.

  • Lars Lohndorf-Larsen Profile Picture
    on at
    RE: How to validate Phone No field

    Hi Nalin,

    Sorry no, in that case you cannot set target to OnPrem. Then your challenge is to not use .net variables. Keep in mind that .al code as default runs server-side, so when hosted on a MS server, it is not a good idea (for MS) to allow access to the local OS. There are built-in datatypes that you can use. For example here, were it uses the built-in Char and Variant datatypes:

                        for i := 1 to StrLen(rec."Phone No.") do begin
                            Char := rec."Phone No."[i];
                            vari := Char;
                            if not Vari.IsInteger then
                                error('Bad number, please enter a better number.')
                        end;
        var
            i: Integer;
            Vari: Variant;
            CharChar;
    I hope you can use that instead?
  • Nalin.karu Profile Picture
    120 on at
    RE: How to validate Phone No field

    Hi Lars,

    Thanks for your explanation and code. appreciate it.

    Regards and Thanks,

    Nalin.

  • Suggested answer
    JAngle Profile Picture
    125 on at
    RE: How to validate Phone No field

    One small consideration here being phone numbers can contain + so just checking for integer won’t be enough. You can can do if not Rec.”Phone No.” in [‘+’, ‘0’ .. ‘ 9’] then

    good explanation on controlling this here: https://youtu.be/cr9UCF-jEz0

  • Suggested answer
    Giambattista Malvasi  Profile Picture
    195 on at
    RE: How to validate Phone No field

    LOCAL PROCEDURE IsNumeric@1(p_gText@1000 : Text) : Boolean;

    VAR

     i@1001 : Integer;

    BEGIN

     FOR i := 1 TO STRLEN(p_gText) DO

       IF (FORMAT(p_gText[i]) IN  ['0'..'9']) OR (FORMAT(p_gText[i]) IN  ['.']) THEN

         IF STRLEN(DELCHR(p_gText, '=', DELCHR(p_gText,'=', '.'))) <= 1 THEN

           EXIT(TRUE)

         ELSE

           EXIT(FALSE)

       ELSE

         EXIT(FALSE);

    END;

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…

Pallavi Phade – Community Spotlight

We are honored to recognize Pallavi Phade as our Community Spotlight honoree for…

Leaderboard > Small and medium business | Business Central, NAV, RMS

#1
Sumit Singh Profile Picture

Sumit Singh 2,039

#2
Rishabh Kanaskar Profile Picture

Rishabh Kanaskar 1,863

#3
YUN ZHU Profile Picture

YUN ZHU 1,736 Super User 2025 Season 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans