Dynamics 365 Business Central: How to prevent users from entering special characters inside an input field

Dynamics 365 Business Central

Hi Readers.
This time, I want to discuss how to prevent users from entering special characters inside an input field in Business Central.
As we all know, there are no any restrictions on the values entered in fields from NAV. For example, I created a new customer called &*””””A@#$%'()|?/. Then I can use this customer to create a new Sales Order and post it.

It went well and the system didn’t stop me. But for some customers, there may be situations where special characters are not allowed to be entered. And if an interface exists, some special characters may not be recognized by other systems. So, We’d better prevent users from entering special characters when the user enters.
In this post, I will share two easy way to do it.
Test Sample:
Table: Customer
Field: Name

Use Text.StrPos Method

StrPos Method: Searches for the first occurrence of substring inside a string.
Syntax: Position := Text.StrPos(String: String, SubString: String)
If SubString cannot be found, then the method returns zero.

1. Create a new Table Extension to extend table Customer, and then add OnBeforeValidate under the field Name field.

2. Create a new local procedure, and add the following code.
If the input field contains *, @, or &, it will prompt an error.

3. Add this local procedure to the OnBeforeValidate trigger.

Test Video:

Find out more about Text.StrPos Method from Microsoft Docs.

Use DelChr Method and StrLen Method

DelChr Method: Deletes chars contained in the which parameter in a string based on the contents on the where parameter.
Syntax: NewString := Text.DelChr(String: String [, Where: String] [, Which: String])
If Where contains =, then all the spaces are deleted from String.
If Where contains <, then all the spaces at the start of String are deleted.
If Where contains >, then all the spaces at the end of String are deleted.
If Where contains any other character, then an error is returned.
If Where is empty, then String is returned unchanged.

StrLen Method: Gets the length of a string you define.
Syntax: Length := Text.StrLen(String: String)

1. Create a new Table Extension to extend table Customer, and then add OnBeforeValidate under the field Name field.

2. Create a new local procedure, and add the following code.
If the special characters defined in SpecialChars are included, it will prompt an error.

    var
        SpecialCharsErr: Label 'You can not enter the special characters!';

    local procedure CheckSpecialChars(Var CustName: Text[100])
    var
        SpecialChars: Label '!|@|#|$|%|&|*|(|)|_|-|+|=|?';
        Len: Integer;
    begin
        Clear(Len);
        Len := StrLen(DelChr(CustName, '=', DelChr(CustName, '=', SpecialChars)));
        if Len > 0 then
            Error(SpecialCharsErr);
    end;

3. Add this local procedure to the OnBeforeValidate trigger.

Test Video:

Find out more about Text.DelChr Method and Text.StrLen Method from Microsoft Docs.

END

Hope this will help.

Thanks for your reading.

ZHU


コメント

Copied title and URL