Dynamics 365 Business Central: How to prohibit users from entering illegal characters such as emoji, Hiragana, kanji, etc.

Dynamics 365 Business Central

Hi, Readers.
Today I would like to talk about how to prohibit users from entering illegal characters such as emoji, Hiragana or kanji in Business Central. In computer science, an illegal character is a character that is not allowed by a certain programming language.
There was an interesting discussion on X(formerly Twitter) before.

For example,

We can use emojis directly in AL code!

And, the Company Badge can also be emojis.

Of course, kanji are also allowed.

So, how to prohibit users from entering illegal characters in some key fields will be a problem. In this post, I will briefly share a method, which may not be the only or easiest method, but I hope it will give you some help.

This time we mainly use the method discussed before, how to extract characters from a string (indexing with []). Let’s see more details:

We can used the method below, [] to convert the string into a single-dimensional array. What was obtained was a number (Character Code).

Rec.Name[i]

For exmaple,

Adatum Corporation:
65 100 97 116 117 109 32 67 111 114 112 111 114 97 116 105 111 110

I found some relevant datasheets in MS Learn (Docs). (Although there are not Business Central documents)
Character set (0 – 127): 7 bit

CodeCharacterCodeCharacterCodeCharacterCodeCharacter
032[space]64@96`
133!65A97a
23466B98b
335#67C99c
436$68D100d
537%69E101e
638&70F102f
73971G103g
8* *40(72H104h
9* *41)73I105i
10* *42*74J106j
1143+75K107k
1244,76L108l
13* *4577M109m
1446.78N110n
1547/79O111o
1648080P112p
1749181Q113q
1850282R114r
1951383S115s
2052484T116t
2153585U117u
2254686V118v
2355787W119w
2456888X120x
2557989Y121y
2658:90Z122z
2759;91[123{
2860<92\124|
2961=93]125}
3062>94^126~
3163?95_127

PS: The values with blanks are control characters, not characters displayed or printed by Windows.

Character set (128 – 255):

CodeCharacterCodeCharacterCodeCharacterCodeCharacter
128160[space]192À224à
129161¡193Á225á
130162¢194Â226â
131ƒ163£195Ã227ã
132164¤196Ä228ä
133165¥197Å229å
134166¦198Æ230æ
135167§199Ç231ç
136ˆ168¨200È232è
137169©201É233é
138Š170ª202Ê234ê
139171«203Ë235ë
140Œ172¬204Ì236ì
141173­205Í237í
142Ž174®206Î238î
143175¯207Ï239ï
144176°208Ð240ð
145177±209Ñ241ñ
146178²210Ò242ò
147179³211Ó243ó
148180´212Ô244ô
149181µ213Õ245õ
150182214Ö246ö
151183·215×247÷
152˜184¸216Ø248ø
153185¹217Ù249ù
154š186º218Ú250ú
155187»219Û251û
156œ188¼220Ü252ü
157189½221Ý253ý
158ž190¾222Þ254þ
159Ÿ191¿223ß255ÿ

PS:
1. Character 160 is a no-break space. Character 173 is a soft hyphen. Some characters aren’t supported by Microsoft Windows (characters 129, 141, 143, 144, and 157).

2. The values in the table are the Windows default. However, values in the ANSI character set above 127 are determined by the code page specific to your operating system.

In addition to common letters and numbers, other special characters, including emoji, Hiragana or kanji, can also be obtained.

If you are interested, you can use the following code to do a simple test.

Source code:

pageextension 50110 CustomerCardExt extends "Customer Card"
{
    actions
    {
        addfirst(Processing)
        {
            action("Get Character Code")
            {
                Caption = 'Get Character Code';
                Image = Refresh;
                ApplicationArea = All;
                Promoted = true;
                PromotedCategory = Process;

                trigger OnAction()
                var
                    i: Integer;
                    CharNo: Integer;
                    Str: Text;
                    Msg: Label 'Customer Name: %1\Character Code: %2';
                begin
                    for i := 1 to strlen(Rec.Name) do begin
                        CharNo := Rec.Name[i];
                        Str += Format(CharNo) + ' ';
                    end;
                    Message(Msg, Rec.Name, Str);
                end;
            }
        }
    }
}

So we can use this number (Character Code) to do some control. Let’s see another specific example. If there is a string number (Character Code) greater than 255 (You can also limit it to 127), an error will be prompted.

Source code:

tableextension 50118 MyExtension extends Customer
{
    fields
    {
        modify(Name)
        {
            trigger OnBeforeValidate()
            var
                i: Integer;
                CharNo: Integer;
            begin
                for i := 1 to strlen(Rec.Name) do begin
                    CharNo := Rec.Name[i];
                    if CharNo > 255 then
                        Error('Invalid character');
                end;
            end;
        }
    }
}

Very simple, give it a try!!!😁

PS:
1. How to validate E-Mail field and Phone field format via AL (Validation rules)

2. Business Central 2023 wave 1 (BC22): Iterating with foreach on Text variables

3. How to get the next or previous letter in the alphabet:

END

Hope this will help.

Thanks for reading.

ZHU

コメント

Copied title and URL