Hi, Readers.
Today I would like to talk about how to validate E-mail field and Phone field format via AL.
As you know, many features, such as Customer Card, Vendor Card, Sales Order, etc., have “E-Mail” and “Phone No.” fields.
For example, Customer Card:
Sales Order:
They are both Text Data Type, but with different ExtendedDatatype Property.
A valid email address consists of an email prefix and an email domain, both in acceptable formats. The prefix appears to the left of the @ symbol. The domain appears to the right of the @ symbol. For example, in the address example@mail.com, “example” is the email prefix, and “mail.com” is the email domain.
When the user enters a value, the system will automatically verify whether it is a valid email address or phone number. And, multiple email addresses can be entered in the “E-Mail” field, these email addresses must be verified separately. More details: How to add multiple email addresses to a customer/vendor/contact
Validation Results
The email address “2q.com” is not valid.
The same goes for the Phone No. field. When you enter letters or unavailable symbols, the system will also automatically check it.
Validation Results
Phone No. must not contain letters in Customer No.=’10000′.
So how is this done? Are there any best practices? Let’s check the standard code first.
“E-Mail” field:
field(102; "E-Mail"; Text[80])
{
Caption = 'Email';
ExtendedDatatype = EMail;
trigger OnValidate()
begin
ValidateEmail();
end;
}
local procedure ValidateEmail()
var
MailManagement: Codeunit "Mail Management";
IsHandled: Boolean;
begin
IsHandled := false;
OnBeforeValidateEmail(Rec, IsHandled, xRec);
if IsHandled then
exit;
if "E-Mail" = '' then
exit;
MailManagement.CheckValidEmailAddresses("E-Mail");
end;
“Phone No.” field:
Char.IsLetter Method: Indicates whether a Unicode character is categorized as a Unicode letter.
field(9; "Phone No."; Text[30])
{
Caption = 'Phone No.';
ExtendedDatatype = PhoneNo;
trigger OnValidate()
var
Char: DotNet Char;
i: Integer;
begin
for i := 1 to StrLen("Phone No.") do
if Char.IsLetter("Phone No."[i]) then
FieldError("Phone No.", PhoneNoCannotContainLettersErr);
end;
}
For the E-Mail field, we can just use the following standard method, MailManagement.ValidateEmailAddressField(“E-Mail”);
codeunit 9520 “Mail Management”:
But for the Phone No. field, the standard uses the DotNet Data type. In the SaaS version, we cannot use the DotNet Data type. (.NET Interoperability is only available on-premise. More details: Getting started with Microsoft .NET Interoperability from AL)
So we need to change the way. Is there any standard method that can be used? Yes. In codeunit 10 “Type Helper”: procedure IsPhoneNumber
Okay, let’s see a simple example.
Great, give it a try!!!😁
Source code:
tableextension 50115 CustomerExt extends Customer
{
fields
{
field(50100; "ZY E-Mail"; Text[80])
{
Caption = 'New Email';
ExtendedDatatype = EMail;
trigger OnValidate()
var
MailManagement: Codeunit "Mail Management";
begin
MailManagement.CheckValidEmailAddresses("ZY E-Mail");
end;
}
field(50101; "ZY Phone No."; Text[30])
{
Caption = 'New Phone No.';
ExtendedDatatype = PhoneNo;
trigger OnValidate()
var
TyperHelper: Codeunit "Type Helper";
begin
if not TyperHelper.IsPhoneNumber("ZY Phone No.") then
FieldError("ZY Phone No.", PhoneNoCannotContainLettersErr);
end;
}
}
var
PhoneNoCannotContainLettersErr: Label 'must not contain letters';
}
pageextension 50118 CustomoerCardExt extends "Customer Card"
{
layout
{
addafter(Name)
{
field("ZY E-Mail"; Rec."ZY E-Mail")
{
ApplicationArea = All;
}
field("ZY Phone No."; Rec."ZY Phone No.")
{
ApplicationArea = All;
}
}
}
}
END
Hope this will help.
Thanks for reading.
ZHU
コメント