返回列表 發帖

[Delphi] 檢查文字方法

// 檢查是否正整數 positive integer
function TMainForm.validate_pno(no: string): boolean;
var dummy:integer;
begin
try
begin
   dummy:=strtoint(no);
   if dummy>0 then result:=true
   else result:= false;
end;
except on E: EConvertError do
   result:=false;
end;
end;


// 檢查是否整數 integer
function TMainForm.validate_no(no: string): boolean;
var dummy:integer;
begin
try
begin
   dummy:=strtoint(no);
   result:=true;
end;
except on E: EConvertError do
   result:=false;
end;
end;


//檢查是否正數字 positive number
function TMainForm.validate_pno(no: string): boolean;
var dummy:integer;
begin
try
begin
   dummy:=strtofloat(no);
   if dummy>0 then result:=true
   else result:= false;
end;
except on E: EConvertError do
   result:=false;
end;
end;


// 檢查是否數字 number
function TMainForm.validate_no(no: string): boolean;
var dummy:integer;
begin
try
begin
   dummy:=strtofloat(no);
   result:=true;
end;
except on E: EConvertError do
   result:=false;
end;
end;


//檢查是否標準 IP 地址格式

function TMainForm.validate_ip(no: string): boolean;
var dummy, dot: integer;
  ip1,ip2,ip3,ip4,strdummy: string;
  result1, result2, result3, result4: boolean;
begin
  result := false;
  trim(no);
  dot := Pos('.',no);
  if dot=0 then exit;
  strdummy := RightStr(no, StrLen(PAnsiChar(no))-dot);
  ip1 := LeftStr(no, dot-1);
  dot := Pos('.',strdummy);
  if dot=0 then exit;
  ip2 := LeftStr(strdummy, dot-1);
  strdummy := RightStr(strdummy, StrLen(PAnsiChar(strdummy))-dot);
  dot := Pos('.',strdummy);
  if dot=0 then exit;
  ip3 := LeftStr(strdummy, dot-1);
  ip4 := RightStr(strdummy, StrLen(PAnsiChar(strdummy))-dot);

  try
  begin
    dummy:=strtoint(ip1);
    if ((dummy>-1) and (dummy <256)) then result1:=true
    else result1:= false;
  end;
  except on E: EConvertError do
    result1:=false;
  end;

    try
  begin
    dummy:=strtoint(ip2);
    if ((dummy>-1) and (dummy <256)) then result2:=true
    else result2:= false;
  end;
  except on E: EConvertError do
    result2:=false;
  end;

  try
  begin
    dummy:=strtoint(ip3);
    if ((dummy>-1) and (dummy <256)) then result3:=true
    else result3:= false;
  end;
  except on E: EConvertError do
    result3:=false;
  end;

  try
  begin
    dummy:=strtoint(ip4);
    if ((dummy>-1) and (dummy <256)) then result4:=true
    else result4:= false;
  end;
  except on E: EConvertError do
    result4:=false;
  end;

  result := result1 and result2 and result3 and result4
end;
Bill Tang     MSN:billtang@openplatform.com.hk
Openplatform Technology Co.,Ltd. 資訊坊科技有限公司  
無線工程施工、方案設計、無線產品、天饋材料、終端設備綜合供應商
Tel: 852-27491011  Fax: 852-81483532

返回列表