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
Code | Character | Code | Character | Code | Character | Code | Character |
---|---|---|---|---|---|---|---|
0 | | 32 | [space] | 64 | @ | 96 | ` |
1 | | 33 | ! | 65 | A | 97 | a |
2 | | 34 | “ | 66 | B | 98 | b |
3 | | 35 | # | 67 | C | 99 | c |
4 | | 36 | $ | 68 | D | 100 | d |
5 | | 37 | % | 69 | E | 101 | e |
6 | | 38 | & | 70 | F | 102 | f |
7 | | 39 | ‘ | 71 | G | 103 | g |
8 | * * | 40 | ( | 72 | H | 104 | h |
9 | * * | 41 | ) | 73 | I | 105 | i |
10 | * * | 42 | * | 74 | J | 106 | j |
11 | | 43 | + | 75 | K | 107 | k |
12 | | 44 | , | 76 | L | 108 | l |
13 | * * | 45 | – | 77 | M | 109 | m |
14 | | 46 | . | 78 | N | 110 | n |
15 | | 47 | / | 79 | O | 111 | o |
16 | | 48 | 0 | 80 | P | 112 | p |
17 | | 49 | 1 | 81 | Q | 113 | q |
18 | | 50 | 2 | 82 | R | 114 | r |
19 | | 51 | 3 | 83 | S | 115 | s |
20 | | 52 | 4 | 84 | T | 116 | t |
21 | | 53 | 5 | 85 | U | 117 | u |
22 | | 54 | 6 | 86 | V | 118 | v |
23 | | 55 | 7 | 87 | W | 119 | w |
24 | | 56 | 8 | 88 | X | 120 | x |
25 | | 57 | 9 | 89 | Y | 121 | y |
26 | | 58 | : | 90 | Z | 122 | z |
27 | | 59 | ; | 91 | [ | 123 | { |
28 | | 60 | < | 92 | \ | 124 | | |
29 | | 61 | = | 93 | ] | 125 | } |
30 | | 62 | > | 94 | ^ | 126 | ~ |
31 | | 63 | ? | 95 | _ | 127 | |
PS: The values with blanks are control characters, not characters displayed or printed by Windows.
Code | Character | Code | Character | Code | Character | Code | Character |
---|---|---|---|---|---|---|---|
128 | € | 160 | [space] | 192 | À | 224 | à |
129 | 161 | ¡ | 193 | Á | 225 | á | |
130 | ‚ | 162 | ¢ | 194 | Â | 226 | â |
131 | ƒ | 163 | £ | 195 | Ã | 227 | ã |
132 | „ | 164 | ¤ | 196 | Ä | 228 | ä |
133 | … | 165 | ¥ | 197 | Å | 229 | å |
134 | † | 166 | ¦ | 198 | Æ | 230 | æ |
135 | ‡ | 167 | § | 199 | Ç | 231 | ç |
136 | ˆ | 168 | ¨ | 200 | È | 232 | è |
137 | ‰ | 169 | © | 201 | É | 233 | é |
138 | Š | 170 | ª | 202 | Ê | 234 | ê |
139 | ‹ | 171 | « | 203 | Ë | 235 | ë |
140 | Œ | 172 | ¬ | 204 | Ì | 236 | ì |
141 | 173 | | 205 | Í | 237 | í | |
142 | Ž | 174 | ® | 206 | Î | 238 | î |
143 | 175 | ¯ | 207 | Ï | 239 | ï | |
144 | 176 | ° | 208 | Ð | 240 | ð | |
145 | ‘ | 177 | ± | 209 | Ñ | 241 | ñ |
146 | ’ | 178 | ² | 210 | Ò | 242 | ò |
147 | “ | 179 | ³ | 211 | Ó | 243 | ó |
148 | ” | 180 | ´ | 212 | Ô | 244 | ô |
149 | • | 181 | µ | 213 | Õ | 245 | õ |
150 | – | 182 | ¶ | 214 | Ö | 246 | ö |
151 | — | 183 | · | 215 | × | 247 | ÷ |
152 | ˜ | 184 | ¸ | 216 | Ø | 248 | ø |
153 | ™ | 185 | ¹ | 217 | Ù | 249 | ù |
154 | š | 186 | º | 218 | Ú | 250 | ú |
155 | › | 187 | » | 219 | Û | 251 | û |
156 | œ | 188 | ¼ | 220 | Ü | 252 | ü |
157 | 189 | ½ | 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
コメント