Problema - Validando Telefone com JavaScript em pagina jsf

2 respostas
K

Ola gente eu estou com um problema ao validar o telefone nesse campo quando eu uso o seguinte javaScript:

function TelefoneFormat(Campo, e) {
	var key = '';
	var len = 0;
	var strCheck = '[telefone removido]';
	var aux = '';
	var whichCode = (window.Event) ? e.which : e.keyCode;
	
	if (whichCode == 13 || whichCode == 8 || whichCode == 0)
	{
		return true;  // Enter backspace ou FN qualquer um que não seja alfa numerico
	}
	key = String.fromCharCode(whichCode);
	if (strCheck.indexOf(key) == -1){
		return false;  //NÃO E VALIDO
	}
	
	aux =  Telefone_Remove_Format(Campo.value);
	
	len = aux.length;
	if(len>=8)
	{
		return false;	//impede de digitar um telefone maior que 10
	}
	aux += key;
	
	Campo.value = Telefone_Mont_Format(aux);
	return false;
}

function  Telefone_Mont_Format(Telefone)
{
	var aux = len = '';
	
	len = Telefone.length;
	if(len<=9)
	{
		tmp = 4;
	}
	else
	{
		tmp = 6;
	}
	
	aux = '';
	for(i = 0; i < len; i++)
	{		
		aux += Telefone.charAt(i);			
		if(i+1==tmp)
		{
			aux += '-';
		}
	}
        
	return aux ;
}

function  Telefone_Remove_Format(Telefone)
{
	var strCheck = '[telefone removido]';
	var len = i = aux = '';
	len = Telefone.length;
	for(i = 0; i < len; i++)
	{
		if (strCheck.indexOf(Telefone.charAt(i))!=-1)
		{
			aux += Telefone.charAt(i);
		}
	}
	return aux;
}
e coloco nesse campo:
<td><input type="text" id="Telefone" value="#{FuncionarioManager.funcionario.telefone}" required="true" size="10" onkeypress="return(TelefoneFormat(this,event))"  /></td>

Ele formata o telefone no formato que quero 2222-2222 so que ele nao vai pro banco, como é que eu faço pro campo que ta retornando no javaScript va para o banco nesse bean que eu tenho??

2 Respostas

stairway

O que não está indo para o banco, a string contendo o telefone ou a formatação?

tenta utilizar o atributo immediate=“true” em seu input, se nao resolber poste o código de seu bean aqui.

[]'s

K
Na verdade eu ate postei o input errado, eh esse aqui:
<td><input jsfc="h:inputText" id="Telefone" onKeyPress="return(TelefoneFormat(this,event))" value="#{FuncionarioManager.funcionario.telefone}" immediate="true" required="true" maxlength="40" /></td>

Nao ta indo nada pro banco com a formatacao, se eu tirar o javaScript ele funciona normal!
O bean eh esse:

package entity;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.Column;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;

@Entity
@NamedQueries(
{
@NamedQuery(name = "Funcionario.recuperarPorNome", query = "SELECT f FROM Funcionario f WHERE f.nome = :nome"),
    @NamedQuery(name = "Funcionario.recuperarPorId", query = "SELECT f FROM Funcionario f WHERE f.id = :id"),    
    @NamedQuery(name = "Funcionario.recuperarFuncionarioLikeNome", query = "SELECT f FROM Funcionario f WHERE f.nome LIKE :nome"),
    @NamedQuery(name = "Funcionario.recuperarTodos", query = "SELECT f FROM Funcionario f ORDER BY f.nome")
    })
public class Funcionario implements Serializable {
    
    @javax.persistence.Id
    @javax.persistence.GeneratedValue(strategy = javax.persistence.GenerationType.AUTO)
    private Long id;
    @Column
    private String nome, email, dataNasc, telefone;
    
    public Funcionario() {
    }
    
    public Long getId() {
        return this.id;
    }
    
    
    public void setId(Long id) {
        this.id = id;
    }
    
    
    @Override
    public int hashCode() {
        int hash = 0;
        hash += (this.getId() != null ? this.getId().hashCode() : 0);
        return hash;
    }
    
    
    @Override
    public boolean equals(Object object) {
        
        if (!(object instanceof Funcionario)) {
            return false;
        }
        Funcionario other = (Funcionario)object;
        if (this.getId() != other.getId() && (this.getId() == null || !this.getId().equals(other.getId()))) return false;
        return true;    }
    
    
    @Override
    public String toString() {
        return "entity.Funcionario[id=" + getId() + "]";
    }
    
    public String getNome() {
        return nome;
    }
    
    public void setNome(String nome) {
        this.nome = nome;
    }
    
    public String getEmail() {
        return email;
    }
    
    public void setEmail(String email) {
        this.email = email;
    }
    
    public String getDataNasc() {
        return dataNasc;
    }
    
    public void setDataNasc(String dataNasc) {
        this.dataNasc = dataNasc;
    }
    
    public String getTelefone() {
        return telefone;
    }
    
    public void setTelefone(String telefone) {
        this.telefone = telefone;
    }
    
}
Criado 11 de janeiro de 2008
Ultima resposta 11 de jan. de 2008
Respostas 2
Participantes 2