TASF
Dezembro 13, 2011, 12:07pm
#1
boa tarde tenho a funcao de JS
function ValidarDados(){
var teste = true;
var saida = "O(s) Seguinte(s) Campo(s) é (são) obrigatório(s):";
if (document.getElementById ('nome').value == ''){
saida += "\nNome";
teste = false;
}
if (document.getElementById ('cpf').value == ''){
saida += "\nCPF Invalido ";
teste = false;
// AKI EU SO POSSO ACEITAR NUMEROS ,COMO POSSO FASER ISSO?
// SE POSSIVEL COMO POSSO ESTAR VALIDANDO O MSMO ???
}
if (document.getElementById ('telefone').value == ''){
saida += "\nTelefone";
teste = false;
}
if (document.getElementById ('rg').value == ''){
saida += "\nRG";
teste = false;
}
if (document.getElementById ('estado_civil').value == ''){
saida += "\nEstado Civil";
teste = false;
}
if (document.getElementById ('ativo').value == '' || document.getElementById ('ativo').value !='1'&& document.getElementById ('ativo').value != '2'){
saida += "\nAtivo (1- ATIVO e 2-INATIVO)";
teste = false;
}
if (document.getElementById ('dt_cadastro').value == ''){
saida += "\nData do cadastro ";
teste = false;
}
if (document.getElementById ('endereco').value == ''){
saida += "\nendereco";
teste = false;
}
if (teste){
return true;
}else{
alert (saida);
return false;
}
}
</script>
aqui eu chamo a funcao
<tr>
<td colspan=2><input type=submit name=cadastra id=cadastra value=Cadastrar onclick="return ValidarDados();" /></td>
</tr>
grato pela ajuda !!
excelente a pergunta do ssh, também não entendi hehe
TASF
Dezembro 13, 2011, 2:14pm
#4
minha duvida e como posso restringir o campo CPF para receber somente numeros ??? e se possivel validar o msmo
Voce pode utilizar a jquery numeric que é bem simples de implementar:
http://www.texotela.co.uk/code/jquery/numeric/
ou usar essa funcao simples abaixo e coloca-la no onkeypress ou onkeyup do input:
function SomenteNumero(e) {
if (window.event){
tecla = e.keyCode;
} else if (e.which){
tecla = e.which;
}
if ( (tecla >= 48 && tecla <= 57)||(tecla == 8 ) ) {
return true;
}else{
return false;
}
}
exemplo input:
<input type="text" name="teste" onkeypress="return SomenteNumero(event)" />
mas a primeira opcao é a melhor e mais profissional
existe também a jquery masked input, você pode até definir a mascara para a input no formato do cpf, é bem legal:
http://digitalbush.com/projects/masked-input-plugin/
espero ter ajudado
abraço
Por que complicar tanto?
Veja a função isNaN (is not a number).
Aqui tem algumas informações úteis:
http://www.w3schools.com/jsref/jsref_isNaN.asp
TASF
Dezembro 13, 2011, 3:22pm
#8
drsmachado nao entendi implementei a funcao so q re da false
var cpf=document.write(isNaN(123)+ "<br />");
var teste = true;
var saida = "O(s) Seguinte(s) Campo(s) é (são) obrigatório(s):";
if (document.getElementById ('nome').value == ''){
saida += "\nNome";
teste = false;
}
if (document.getElementById ('cpf').value == ''||document.getElementById ('cpf').value != cpf){
saida += "\nCPF Invalido ";
teste = false;
}
???
ssh
Dezembro 13, 2011, 3:30pm
#9
Como você está chamando a função?
qual função está utilizando? poste o máximo de informação para que as pessoas possam lhe ajudar melhor.
existem várias funções javascript(jquery) que montam as macaras para o seu campo.
Por exemplo: quando você vai digitando ele vai adicionando o "/"ou o “.” automáticamente. de uma pesquisada não é dificil.
TASF
Dezembro 13, 2011, 3:39pm
#10
fabio tbm tentei sua ideia mas nao fuii feliz
fiz assim meu input
<tr>
<td><b>CPF</b></td>
<td><input type=text name=cpf id=cpf onkeypress="return ValidarDados(event)"/></td>
<tr>
o meu script
function ValidarDados(){
var teste = true;
var saida = "O(s) Seguinte(s) Campo(s) é (são) obrigatório(s):";
if (document.getElementById ('nome').value == ''){
saida += "\nNome";
teste = false;
}
if (document.getElementById ('cpf').value != ''){
if (window.event){
tecla = e.keyCode;
} else if (e.which){
tecla = e.which;
}
}
if ( (tecla >= 48 && tecla <= 57)||(tecla == 8 ) ) {
return true;
}else{
return false;
}
if (document.getElementById ('telefone').value == ''){
saida += "\nTelefone";
teste = false;
}
if (document.getElementById ('rg').value == ''){
saida += "\nRG";
teste = false;
}
if (document.getElementById ('estado_civil').value == ''){
saida += "\nEstado Civil";
teste = false;
}
if (document.getElementById ('ativo').value == '' || document.getElementById ('ativo').value !='1'&& document.getElementById ('ativo').value != '2'){
saida += "\nAtivo (1- ATIVO e 2-INATIVO)";
teste = false;
}
if (document.getElementById ('dt_cadastro').value == ''){
saida += "\nData do cadastro ";
teste = false;
}
if (document.getElementById ('endereco').value == ''){
saida += "\nendereco";
teste = false;
}
if (teste){
return true;
}else{
alert (saida);
return false;
}
}
</script>
detalhe nesse script esta validando todos os campos ,porem o cpf qro q receba so numeros nao to conseguindo…
Segue exemplo da função.
Provavelmente você se confundiu e, ao utilziar isNaN com número esperava retorno true, quando, isNaN vai retornar
isNaN(num) = true quando num NÃO é um número.
isNaN(num) = false quando num É um número.
<html>
<head>
<script>
function verificaNumero(){
var num = document.getElementById('cpf').value;
if(isNaN(num)){
alert(num + ' não é um número, o retorno da função isNaN é verdadeiro');
}else{
alert(num + ' é um número, o retorno da função isNaN é falso');
}
}
</script>
</head>
<body>
CPF <input type="text" name="cpf" id="cpf"/>
<br/>
<input type="button" onclick="verificaNumero();" value="Testar"/>
</body>
</html>