首页  编辑  

检查一个字符串是否是合法???

Tags: /超级猛料/String.字符串处理/   Date Created:
是否是合法的E-Mail地址:
function MailURLMayBeInvalid(const s: string): Boolean;
var
 i: Integer;
 c: string;
begin // ' ', ä, ö, ü, ß, [, ], (, ), : in EMail-Address
 Result := (Trim(s) = '') or (Pos(' ', AnsiLowerCase(s)) > 0) or
   (Pos('ä', AnsiLowerCase(s)) > 0) or (Pos('ö', AnsiLowerCase(s)) > 0) or
   (Pos('ü', AnsiLowerCase(s)) > 0) or (Pos('ß', AnsiLowerCase(s)) > 0) or
   (Pos('[', AnsiLowerCase(s)) > 0) or (Pos(']', AnsiLowerCase(s)) > 0) or
   (Pos('(', AnsiLowerCase(s)) > 0) or (Pos(')', AnsiLowerCase(s)) > 0) or
   (Pos(':', AnsiLowerCase(s)) > 0);
 if Result then Exit; // @ not in EMail-Address;
 i      := Pos('@', s);
 Result := (i = 0) or (i = 1) or (i = Length(s));
 if Result then Exit;
 Result := (Pos('@', Copy(s, i + 1, Length(s) - 1)) > 0);
 if Result then Exit; // Domain <= 1
 c      := Copy(s, i + 1, Length(s));
 Result := Length(c) <= 1;
 if Result then Exit;
 i      := Pos('.', c);
 Result := (i = 0) or (i = 1) or (i = Length(c));
end;
是否是合法的IP地址:
function IsWrongIP(ip: string): Boolean;
var
 z, i: byte;
 st: array[1..3] of byte;
const
 ziff = ['0'..'9'];
begin
 st[1]  := 0;
 st[2]  := 0;
 st[3]  := 0;
 z      := 0;
 Result := False;
 for i := 1 to Length(ip) do if ip[i] in ziff then
 else
 begin
   if ip[i] = '.' then
   begin
     Inc(z);
     if z < 4 then st[z] := i
     else
     begin
       IsWrongIP := True;
       Exit;
     end;
   end
   else
   begin
     IsWrongIP := True;
     Exit;
   end;
 end;
 if (z <> 3) or (st[1] < 2) or (st[3] = Length(ip)) or (st[1] + 2 > st[2]) or
   (st[2] + 2 > st[3]) or (st[1] > 4) or (st[2] > st[1] + 4) or (st[3] > st[2] + 4) then
 begin
   IsWrongIP := True;
   Exit;
 end;
 z := StrToInt(Copy(ip, 1, st[1] - 1));
 if (z > 255) or (ip[1] = '0') then
 begin
   IsWrongIP := True;
   Exit;
 end;
 z := StrToInt(Copy(ip, st[1] + 1, st[2] - st[1] - 1));
 if (z > 255) or ((z <> 0) and (ip[st[1] + 1] = '0')) then
 begin
   IsWrongIP := True;
   Exit;
 end;
 z := StrToInt(Copy(ip, st[2] + 1, st[3] - st[2] - 1));
 if (z > 255) or ((z <> 0) and (ip[st[2] + 1] = '0')) then
 begin
   IsWrongIP := True;
   Exit;
 end;
 z := StrToInt(Copy(ip, st[3] + 1, Length(ip) - st[3]));
 if (z > 255) or ((z <> 0) and (ip[st[3] + 1] = '0')) then
 begin
   IsWrongIP := True;
   Exit;
 end;
end;