Função para validar Celular
Olá pessoal.
Dica simples mas bem útil para a validação de números de celulares.
Esta dica foi baseada na função enviada por Eduardo Souza.
É necessário dar uses de System.RegularExpressions.
Vamos ao código:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| function IsValidCelular(aCelNumber: string): boolean;
var
ipRegExp, vFone: string;
begin
Result := False;
{ Recuperando somente os numeros }
vFone := GetStrNumber(aCelNumber);
try
ipRegExp := '^[1-9]{2}(?:[6-9]|9[1-9])[0-9]{3}[0-9]{4}$';
if TRegEx.IsMatch(vFone, ipRegExp) then
Result := True;
except
on E: Exception do
ShowMessage(E.ClassName + ' : ' + E.Message);
end;
end; |
function IsValidCelular(aCelNumber: string): boolean;
var
ipRegExp, vFone: string;
begin
Result := False;
{ Recuperando somente os numeros }
vFone := GetStrNumber(aCelNumber);
try
ipRegExp := '^[1-9]{2}(?:[6-9]|9[1-9])[0-9]{3}[0-9]{4}$';
if TRegEx.IsMatch(vFone, ipRegExp) then
Result := True;
except
on E: Exception do
ShowMessage(E.ClassName + ' : ' + E.Message);
end;
end;
Exemplo 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 IsValidCelular('(55) 9963-7552') then
ShowMessage('Sim')
else
ShowMessage('Nao');
if IsValidCelular('(55) 99963-7552') then
ShowMessage('Sim')
else
ShowMessage('Nao');
if IsValidCelular('(550) 3443-7552') then
ShowMessage('Sim')
else
ShowMessage('Nao');
if IsValidCelular('(55) 3443-7552') then
ShowMessage('Sim')
else
ShowMessage('Nao');
end; |
procedure TForm1.Button1Click(Sender: TObject);
begin
if IsValidCelular('(55) 9963-7552') then
ShowMessage('Sim')
else
ShowMessage('Nao');
if IsValidCelular('(55) 99963-7552') then
ShowMessage('Sim')
else
ShowMessage('Nao');
if IsValidCelular('(550) 3443-7552') then
ShowMessage('Sim')
else
ShowMessage('Nao');
if IsValidCelular('(55) 3443-7552') then
ShowMessage('Sim')
else
ShowMessage('Nao');
end;
Neste exemplo estou fazendo uso da função GetStrNumber
que está disponível neste link:
showdelphi.com.br/dica-como-pegar-somente-os-numeros-de-uma-string-delphi/
Dúvidas ou sugestões, deixe o seu comentário!
-
Giovani Da Cruz
-
7.981 views
- 0 comentários
- 27 de maio de 2017
Está gostando do conteúdo? Considere pagar um cafezinho para nossa equipe!
Posts Relacionados - Continue Aprendendo