unit uPhoneNumberCheck;

interface

uses
  SysUtils, Classes,Vcl.Dialogs;

type
  TPhoneNumberCheck = class(TDataModule)
  private
    function IsPhoneNumberOK2(PhoneNumber: string; Country: integer; var ResultPhoneNumber,
      ErrorMessage: string): Boolean;
    function IsFirstSymbolsOk(CorrectSymbols, SourceString: string; var ErrorMessage: string): boolean;
    function GatherAndCheckPhoneNumber(PhoneNumberSource: string; var ResultPhoneNumber: string;
      CorrectDigitsNumber: Integer; var ErrorMessage:string): boolean;
    { Private declarations }




  public
    { Public declarations }

      function IsPhoneNumberOK(PhoneNumber,Country:string;var
      ResultPhoneNumber:string;
      var ErrorMessage:string):Boolean;
      function IsItDigit(someChar: char): Boolean;

  end;

function PhoneNumberCheck: TPhoneNumberCheck;

implementation

{$R *.dfm}

uses
  UniGUIVars, uniGUIMainModule, MainModule;

function PhoneNumberCheck: TPhoneNumberCheck;
begin
  Result := TPhoneNumberCheck(UniMainModule.GetModuleInstance(TPhoneNumberCheck));
end;

{ TPhoneNumberCheck }

function TPhoneNumberCheck.IsItDigit(someChar:char): Boolean;
begin
 if not (someChar in ['0'..'9']) then Result:=false else Result:=true;
end;

function TPhoneNumberCheck.IsPhoneNumberOK(
PhoneNumber,Country: string;
var ResultPhoneNumber:string;
var ErrorMessage:string
 ): Boolean;

var MaxNumberOfDigitsInNumber:integer;
  i: Integer;
  SomeLength: Integer;

begin
//

if Country=COUNTRY_RUSSIA then
 begin

 // check first symbols: is first symbol +, and second symbol 7, must be +7
   if   Pos('+7',PhoneNumber)<>1 then
 begin
 Result:=false;
 ErrorMessage:='Номер должен начинаться с +7';
 Exit;
 end;


 //gathering digits from string
  ResultPhoneNumber:='';
  for i := 0 to PhoneNumber.Length do
  if IsItDigit(PhoneNumber[i]) then
  ResultPhoneNumber:=ResultPhoneNumber+PhoneNumber[i];//ResultPhoneNumber+'chr'+'('+PhoneNumber[i]+')';


  MaxNumberOfDigitsInNumber:=11; // 79163080631 << 11 digits


 // check phone number length
  if  ResultPhoneNumber.Length=MaxNumberOfDigitsInNumber
  then Result:=true else

  begin
  if ResultPhoneNumber.Length>MaxNumberOfDigitsInNumber then
  ErrorMessage:='Введены лишние цифры' else

  if ResultPhoneNumber.Length<MaxNumberOfDigitsInNumber then
  ErrorMessage:='Недостаточно цифр. Введено '+ResultPhoneNumber.Length.ToString+' из '
  +MaxNumberOfDigitsInNumber.ToString+' цифр';

  Result:=false;
  end;



 end;




 if Country=COUNTRY_UKRAINE then
 begin

 // check first symbols: is first symbol +, and second symbol 7, must be +7
   if   Pos('+38',PhoneNumber)<>1 then
 begin
 Result:=false;
 ErrorMessage:='Номер должен начинаться с +38';
 Exit;
 end;


 //gathering digits from string
  ResultPhoneNumber:='';
  for i := 0 to PhoneNumber.Length do
  if IsItDigit(PhoneNumber[i]) then
  ResultPhoneNumber:=ResultPhoneNumber+PhoneNumber[i];//ResultPhoneNumber+'chr'+'('+PhoneNumber[i]+')';


  MaxNumberOfDigitsInNumber:=12; // +38(073)7232323 << 12 digits


 // check phone number length
  if  ResultPhoneNumber.Length=MaxNumberOfDigitsInNumber
  then Result:=true else

  begin
  if ResultPhoneNumber.Length>MaxNumberOfDigitsInNumber then
  ErrorMessage:='Введены лишние цифры' else

  if ResultPhoneNumber.Length<MaxNumberOfDigitsInNumber then
  ErrorMessage:='Недостаточно цифр. Введено '+ResultPhoneNumber.Length.ToString+' из '
  +MaxNumberOfDigitsInNumber.ToString+' цифр';

  Result:=false;
  end;



 end;



 if Country=COUNTRY_KAZAKHSTAN then
 begin

 // check first symbols: is first symbol +, and second symbol 7, must be +7
   if   Pos('+7',PhoneNumber)<>1 then
 begin
 Result:=false;
 ErrorMessage:='Номер должен начинаться с +7';
 Exit;
 end;


 //gathering digits from string
  ResultPhoneNumber:='';
  for i := 0 to PhoneNumber.Length do
  if IsItDigit(PhoneNumber[i]) then
  ResultPhoneNumber:=ResultPhoneNumber+PhoneNumber[i];//ResultPhoneNumber+'chr'+'('+PhoneNumber[i]+')';


  MaxNumberOfDigitsInNumber:=11; // +77710009998 << 11 digits


 // check phone number length
  if  ResultPhoneNumber.Length=MaxNumberOfDigitsInNumber
  then Result:=true else

  begin
  if ResultPhoneNumber.Length>MaxNumberOfDigitsInNumber then
  ErrorMessage:='Введены лишние цифры' else

  if ResultPhoneNumber.Length<MaxNumberOfDigitsInNumber then
  ErrorMessage:='Недостаточно цифр. Введено '+ResultPhoneNumber.Length.ToString+' из '
  +MaxNumberOfDigitsInNumber.ToString+' цифр';

  Result:=false;
  end;



 end;



end;







function TPhoneNumberCheck.IsPhoneNumberOK2(
PhoneNumber: string;
Country:Integer;

var ResultPhoneNumber:string;
var ErrorMessage:string
 ): Boolean;

var i: Integer;
    InternationalCode:integer;


begin
//

Result:=false;

 case Country of

 0: begin

    if not IsFirstSymbolsOk('+7',PhoneNumber,ErrorMessage) then Exit;

    Result:=GatherAndCheckPhoneNumber();



    end;
 end;

 end;




function TPhoneNumberCheck.GatherAndCheckPhoneNumber(

PhoneNumberSource:string;
var ResultPhoneNumber:string;
CorrectDigitsNumber:Integer;
var ErrorMessage:string

):boolean;
var
  i: Integer;



begin

//

  Result:=false;

  ResultPhoneNumber:='';
  for i := 1 to PhoneNumberSource.Length do
  if IsItDigit(PhoneNumberSource[i]) then
  ResultPhoneNumber:=ResultPhoneNumber+PhoneNumberSource[i];//



 // check phone number length
  if  ResultPhoneNumber.Length=CorrectDigitsNumber
  then Result:=true else

  begin
  if ResultPhoneNumber.Length>CorrectDigitsNumber then
  ErrorMessage:='Введены лишние цифры' else

  if ResultPhoneNumber.Length<CorrectDigitsNumber then
  ErrorMessage:='Недостаточно цифр. Введено '+ResultPhoneNumber.Length.ToString+' из '
  +CorrectDigitsNumber.ToString+' цифр';

  Result:=false;
  end;


end;


function TPhoneNumberCheck.
IsFirstSymbolsOk(CorrectSymbols,SourceString:string; var ErrorMessage:string ):boolean;

begin
  //

        // check first symbols: is first symbol +, and second symbol 7, must be +7
       if  Pos(CorrectSymbols,SourceString)<>1 then
       begin
       Result:=false;
       ErrorMessage:='Номер должен начинаться с '+CorrectSymbols;
       end else Result:=True;

end;


initialization
  RegisterModuleClass(TPhoneNumberCheck);

end.
