Created
February 17, 2014 19:14
-
-
Save pedromenezes/9057033 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<input type="text" name="cpf" id="cpf" placeholder="CPF"></input> | |
<input type="text" name="nome" id="nome" placeholder="Nome" disabled="disabled"></input> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script src="http://code.jquery.com/jquery-1.10.2.min.js"></script> | |
<script src="http://irql.bipbop.com.br/js/jquery.bipbop.min.js"></script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// valida a formatação do CPF. pontos e traços são opcionais | |
function validCPF (cpf) { | |
return cpf.match(/^\d{3}\.?\d{3}\.?\d{3}\-?\d{2}$/); | |
} | |
// quando o usuário digitar no campo CPF | |
$('#cpf').keyup(function(){ | |
var cpf = this.value; | |
// se o CPF tiver formatação válida | |
if (validCPF(cpf)) { | |
// consulta a API da BIPBOP com uma chave gratuita | |
// a constante BIPBOP_FREE é uma chave da BIPBOP e | |
// é definida ao carregar a biblioteca JS da BIPBOP | |
$().bipbop("SELECT FROM 'BIPBOPJS'.'CPFCNPJ'", BIPBOP_FREE, { | |
// passando o CPF digitado | |
data: { documento: cpf }, | |
success: function(data) { | |
// define a variável "nome" com | |
// o nome da pessoa física associada ao CPF | |
var nome = $(data).find("body nome").text(); | |
// muda o campo "nome" para o nome do dono do CPF | |
$("#nome").val(nome); | |
} | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment