Como verificar se uma string pode ser um IP válido?
Esta função verifica se uma string informada poderia ser um IP válido.
Formato de exemplo: 192.168.10.200.
É necessário estar declarado SysUtils na seção uses,
em versões unicode declare System.SysUitls.
Segue o código fonte da função:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
| function IsWrongIP(ip: string): Boolean;
var
z, i: Integer;
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; |
function IsWrongIP(ip: string): Boolean;
var
z, i: Integer;
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;
Exemplos de uso:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
| procedure TForm1.Button1Click(Sender: TObject);
begin
if IsWrongIP('192.168.50.20') then
ShowMessage('O IP NÃO é válido!')
else
ShowMessage('O IP é válido!');
if IsWrongIP('192.168.50.270') then
ShowMessage('O IP NÃO é válido!')
else
ShowMessage('O IP é válido!');
if IsWrongIP('192.168 .20') then
ShowMessage('O IP NÃO é válido!')
else
ShowMessage('O IP é válido!');
if IsWrongIP('dsa.dsad.sada') then
ShowMessage('O IP NÃO é válido!')
else
ShowMessage('O IP é válido!');
end; |
procedure TForm1.Button1Click(Sender: TObject);
begin
if IsWrongIP('192.168.50.20') then
ShowMessage('O IP NÃO é válido!')
else
ShowMessage('O IP é válido!');
if IsWrongIP('192.168.50.270') then
ShowMessage('O IP NÃO é válido!')
else
ShowMessage('O IP é válido!');
if IsWrongIP('192.168 .20') then
ShowMessage('O IP NÃO é válido!')
else
ShowMessage('O IP é válido!');
if IsWrongIP('dsa.dsad.sada') then
ShowMessage('O IP NÃO é válido!')
else
ShowMessage('O IP é válido!');
end;
-
InfusTec
-
3.725 views
- 0 comentários
- 13 de abril de 2015
Está gostando do conteúdo? Considere pagar um cafezinho para nossa equipe!
Posts Relacionados - Continue Aprendendo