Usamos cookies para medir audiência e melhorar sua experiência. Você pode aceitar ou recusar a qualquer momento. Veja sobre o iMasters.
Galera, estou tentando fazer uma validação linha a linha de um formulario. A ideia consiste em validar um input quando ele perde o foco.
A objeto abaixo pega todos os inputs do form, e para cada um, verifica se o campo está correto. Se nao estiver muda o background do input.
function ValidaForm(formID) { this.exceptionMessage = new Array(); this.exceptionList = new Array(); this.formID = formID; this.campoVazio = function(input) { var s = input.value; if (s != null) { while (s[0] == ' ') s = s.substring(1,s.length); if (s.length > 1) { while (s[(s.length-1)] == ' ') s = s.substring(s.length-1,0); } var node = document.getElementsByName(input.name); node.item(0).value = s; } else return false; return (s.length == 0) } this.setExceptionList = function() { this.exceptionList[0] = "O sistema identificou o(s) seguinte(s) erros(s):<br><br>" this.exceptionList[1] = "- Login Inválido<br>"; this.exceptionList[2] = "- Email Inválido - Formato: usuario@dominio.uf<br>"; this.exceptionList[3] = "- Confirmação do e-mail inválida ou em braco<br>"; this.exceptionList[4] = "- Senha Inválida<br>"; this.exceptionList[5] = "- Confirmação de Senha difere da senha digitada<br>"; } this.getInputException = function(input) { var i; switch(input.name) { case "login": i=1; break; case "email": i=2; break; case "confirma_email": i=3; break; case "senha": i=4; break; case "confirma_senha": i=5; break; } if (this.campoVazio(input)) { this.exceptionMessage[0] = this.exceptionList[0]; this.exceptionMessage[i] = this.exceptionList[i]; input.style.backgroundColor = "#FF0000"; } else input.style.backgroundColor = "#FFFFFF"; } this.setInputsValidation = function() { var formNode = document.getElementById(this.formID); var inputList = formNode.getElementsByTagName("input"); var i; this.setExceptionList(); for(i=0;i<(inputList.length);i++) { inputList[i].onblur = this.getInputException(inputList[i]); } }}HTML de exemplo
<html><head><title></title><script language="JavaScript" src="validaform.js"></script><script></script></head><body onLoad="var blabla = new ValidaForm('hi');blabla.setInputsValidation();"><form id="hi"> <input type="text" name="login" \> <input type="text" name="senha" \></form></body></html>O problema é que quando atribuo a funcao getInputException a cada input (no comando inputList*.onblur = this.getInputException(inputList**);) ele só o faz na hora que o objeto é criado, e nao atribui definitivamente aos inputs essa caracteristica.(no ie ele só atribui essa caracteristica ao primeiro input).** *
Alguem sabe me dizer pq isso acontece??
vlw !!
Carregando comentários...