Ah, for validating a specific stored format like 00386xxYYYzzz, here are some Delphi approaches:
- Regular Expression Validation:
function IsValidFormat(const Value: string): Boolean;
begin
Result := TRegEx.IsMatch(Value, '^00386\d{2}[A-Z]{3}\d{3}$');
end;
procedure TForm1.DataSetBeforePost(DataSet: TDataSet);
var
FormatField: TField;
begin
FormatField := DataSet.FieldByName('YourFieldName');
if not IsValidFormat(FormatField.AsString) then
begin
ShowMessage('Invalid format. Must be 00386xxYYYzzz');
Abort;
end;
end;
- Manual String Validation:
function ValidateSpecificFormat(const Value: string): Boolean;
begin
// Check total length
if Length(Value) <> 11 then
Exit(False);
// Check prefix
if Copy(Value, 1, 5) <> '00386' then
Exit(False);
// Check middle part (xx) - two digits
if not TryStrToInt(Copy(Value, 6, 2), 0) then
Exit(False);
// Check middle part (YYY) - three uppercase letters
if not (
(Copy(Value, 8, 1)[1] in ['A'..'Z']) and
(Copy(Value, 9, 1)[1] in ['A'..'Z']) and
(Copy(Value, 10, 1)[1] in ['A'..'Z'])
) then
Exit(False);
// Check last part (zzz) - three digits
if not TryStrToInt(Copy(Value, 11, 3), 0) then
Exit(False);
Result := True;
end;
procedure TForm1.DataSetBeforePost(DataSet: TDataSet);
var
FormatField: TField;
begin
FormatField := DataSet.FieldByName('YourFieldName');
if not ValidateSpecificFormat(FormatField.AsString) then
begin
ShowMessage('Invalid format. Must be 00386xxYYYzzz');
Abort;
end;
end;
- Comprehensive Validation Method:
type
TFormatValidationResult = (
fvValid,
fvInvalidLength,
fvInvalidPrefix,
fvInvalidMiddleDigits,
fvInvalidMiddleLetters,
fvInvalidLastDigits
);
function DetailedFormatValidation(const Value: string): TFormatValidationResult;
begin
// Length check
if Length(Value) <> 11 then
Exit(fvInvalidLength);
// Prefix check
if Copy(Value, 1, 5) <> '00386' then
Exit(fvInvalidPrefix);
// Middle digits check
if not TryStrToInt(Copy(Value, 6, 2), 0) then
Exit(fvInvalidMiddleDigits);
// Middle letters check
if not (
(Copy(Value, 8, 1)[1] in ['A'..'Z']) and
(Copy(Value, 9, 1)[1] in ['A'..'Z']) and
(Copy(Value, 10, 1)[1] in ['A'..'Z'])
) then
Exit(fvInvalidMiddleLetters);
// Last digits check
if not TryStrToInt(Copy(Value, 11, 3), 0) then
Exit(fvInvalidLastDigits);
Result := fvValid;
end;
procedure TForm1.DataSetBeforePost(DataSet: TDataSet);
var
FormatField: TField;
ValidationResult: TFormatValidationResult;
begin
FormatField := DataSet.FieldByName('YourFieldName');
ValidationResult := DetailedFormatValidation(FormatField.AsString);
case ValidationResult of
fvValid:
; // Do nothing, record is valid
fvInvalidLength:
ShowMessage('Invalid length. Must be 11 characters.');
fvInvalidPrefix:
ShowMessage('Invalid prefix. Must start with 00386');
fvInvalidMiddleDigits:
ShowMessage('Invalid middle digits. Must be two numbers.');
fvInvalidMiddleLetters:
ShowMessage('Invalid middle letters. Must be three uppercase letters.');
fvInvalidLastDigits:
ShowMessage('Invalid last digits. Must be three numbers.');
end;
if ValidationResult <> fvValid then
Abort;
end;