Dynamics 365 Business Central: Check whether the given character is a digit or not

Dynamics 365 Business Central

Hi, Readers.
Today I would like to share another mini tip about Business Central, how to check whether the given character is a digit or not.
The first thing to note is that Digit is not Number. Digits are symbols 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 used to represent different values of numbers in math. A number is an amount of something. For example, the number 23 is written with two digits, 2 and 3.
We have discussed the following two things before:
How to validate E-Mail field and Phone field format via AL (Validation rules)
How to prohibit users from entering illegal characters such as emoji, Hiragana, kanji, etc.

But this time I want to share an easier way. There are two standard methods to do this.
codeunit 10 “Type Helper”: IsDigit

table 79 “Company Information”: IsDigit

How to use it? Let’s see two simple examples.
1. Checks each character in the user-entered value (Address 2 on Customer Card) to see if it is a digit

Test video:

Source code:

pageextension 50120 CustomerCardExt extends "Customer Card"
{
    layout
    {
        modify("Address 2")
        {
            trigger OnAfterValidate()
            var
                TypeHelper: Codeunit "Type Helper";
                CharNo: Integer;
                i: Integer;
            begin
                for i := 1 to StrLen(Rec."Address 2") do begin
                    CharNo := Rec."Address 2"[i];
                    if TypeHelper.IsDigit(CharNo) then
                        Message('%1 is a digit', Rec."Address 2"[i])
                    else
                        Message('%1 is not a digit', Rec."Address 2"[i]);
                end;
            end;
        }
    }
}

2. Remove all digits from the text.

Test video:

Source code:

pageextension 50120 CustomerCardExt extends "Customer Card"
{
    layout
    {
        modify("Address 2")
        {
            trigger OnAfterValidate()
            var
                TypeHelper: Codeunit "Type Helper";
                CharNo: Integer;
                i: Integer;
                String: Text[100];
            begin
                String := '';
                for i := 1 to StrLen(Rec."Address 2") do begin
                    CharNo := Rec."Address 2"[i];
                    if TypeHelper.IsDigit(CharNo) then
                        String += Rec."Address 2"[i];
                end;
                Rec."Address 2" := DelChr(Rec."Address 2", '=', String);
            end;
        }
    }
}

Very simple, give it a try!!!😁

END

Hope this will help.

Thanks for reading.

ZHU

コメント

Copied title and URL