<html>
<head>
<title>Teste de validação</title>
<script>
function valida_generico(bot){
/* LEGENDA
se tiver ID COM #01 verifica se há alguma coisa digitada! ex: Id = "NOME#01"
se tiver ID COM #02 verifica se há alguma alternativa selecionada em um select normal! ex: Id = "CATEGORIA#02"
se tiver ID COM #03 comparação de valor com outro campo ! ex: Id = "SENHA#03|vc_conf_senha"
se tiver ID COM #04 verifica se há alguma alternativa selecionada em um select multiple! ex: Id = "CATEGORIA#04"
se tiver ID COM #05 verifica se o E-Mail digitado é válido ex: Id = "E-MAIL#05"
se tiver ID COM #06 verifica se o campo tem x caracteres digitados ex: Id = "CEP#06|9"
*/
for (i = 0; i < document.frm.elements.length; i++){
if (document.frm.elements[i].id.indexOf("#01") != -1){
pos = document.frm.elements[i].id.indexOf("#")
if(document.frm.elements[i].value == ''){
alert('Por favor preencha o campo '+document.frm.elements[i].id.substring(0,pos)+'!')
document.frm.elements[i].focus();
return false;
}
}
if (document.frm.elements[i].id.indexOf("#02") != -1){
if(document.frm.elements[i].value == '0'){
alert('Por favor selecione um(a) '+document.frm.elements[i].id.substring(0,eval(document.frm.elements[i].id.length-3))+'!')
document.frm.elements[i].focus();
return false;
}
}
if (document.frm.elements[i].id.indexOf("#03") != -1){
pos = document.frm.elements[i].id.indexOf("#03")
if(document.frm.elements[i].value == ''){
alert('Por favor preencha o campo '+document.frm.elements[i].id.substring(0,pos)+'!')
document.frm.elements[i].focus();
return false;
}
valor = document.frm.elements[i].id.substring(pos+4,document.frm.elements[i].id.length)
comparacao = eval('document.frm.'+valor+'.value')
id = eval('document.frm.'+valor+'.id')
if (document.frm.elements[i].value != comparacao){
alert('Os campos '+document.frm.elements[i].id.substring(0,pos)+' e '+id.substring(0,(id.length - 3))+' não batem!')
document.frm.elements[i].focus();
return false;
}
}
if (document.frm.elements[i].id.indexOf("#04") != -1){
if(document.frm.elements[i].value.length == 0){
alert('Por favor selecione um(a) '+document.frm.elements[i].id.substring(0,eval(document.frm.elements[i].id.length-3))+'!')
document.frm.elements[i].focus();
return false;
}
}
if (document.frm.elements[i].id.indexOf("#05") != -1){
vc_erro = '';
if (document.frm.elements[i].value.length != 0) {
invalidChars = "/:,;!?<>*&^%$#(){}|~`"
atPos = document.frm.elements[i].value.indexOf("@",1)
periodPos = document.frm.elements[i].value.indexOf(".",atPos)
for (i=0; i<invalidChars.lenght; i++) {
badChar = invalidChars.charAt(i)
if (document.frm.elements[i].value.length.indexOf(badChar,0) > -1) {
vc_erro = '- O E-MAIL digitado é inválido.\n';
}
}
if (atPos == -1) {
vc_erro = '- O E-MAIL digitado é inválido.\n';
}
if(document.frm.elements[i].value.indexOf("@",atPos+1) > -1) {
vc_erro = '- O E-MAIL digitado é inválido.\n';
}
if (periodPos == -1) {
vc_erro = '- O E-MAIL digitado é inválido.\n';
}
if ((periodPos+3) > document.frm.elements[i].value.length) {
vc_erro = '- O E-MAIL digitado é inválido.\n';
}
} else {
vc_erro = vc_erro + '- Digite o E-MAIL.\n';
}
if (vc_erro.length > 0){
alert(vc_erro);
document.frm.elements[i].focus();
return false;
}
}
if (document.frm.elements[i].id.indexOf("#06") != -1){
pos = document.frm.elements[i].id.indexOf("#06")
if(document.frm.elements[i].value == ''){
alert('Por favor preencha o campo '+document.frm.elements[i].id.substring(0,pos)+'!')
document.frm.elements[i].focus();
return false;
}
tamanho = document.frm.elements[i].id.substring(pos+4,document.frm.elements[i].id.length)
if (document.frm.elements[i].value.length != tamanho){
alert('O campo ' + document.frm.elements[i].id.substring(0,pos) + ' deve ter ' + tamanho + ' caracteres!')
document.frm.elements[i].focus();
return false;
}
}
}
bot.value = "...Carregando";
bot.disabled = true;
document.frm.submit();
}
</script>
</head>
<body>
<%
if len(request("vc_nome")) = 0 then
%>
<form name="frm" action="teste.asp">
<table align="center" cellpadding="3" cellspacing="1" border="1" bordercolorlight="#999999" bordercolordark="#FBFBFB">
<tr bgcolor="#FBFBFB">
<td colspan="2" align="center"><b>Validação Genérica</b></td>
</tr>
<tr>
<td align="right"><b>Nome:</b> </td>
<td><input type="text" name="vc_nome" id="NOME#01#05" style="background-color:#FBFBFB; border-style:groove"></td>
</tr>
<tr bgcolor="#FBFBFB">
<td align="right"><b>Categoria:</b> </td>
<td><select name="vc_categoria" id="CATEGORIA#02" style="background-color:#FBFBFB; border-style:groove">
<option value="0">Categoria nula</option>
<option value="1">Categoria válida</option>
</select></td>
</tr>
<tr>
<td align="right"><b>Senha:</b> </td>
<td><input type="text" name="vc_senha" style="background-color:#FBFBFB; border-style:groove" id="SENHA#03|vc_conf_senha"></td>
</tr>
<tr bgcolor="#FBFBFB">
<td align="right"><b>Confirmação:</b> </td>
<td><input type="text" name="vc_conf_senha" style="background-color:#FBFBFB; border-style:groove" id="CONFIRMAÇÃO DE SENHA#01"></td>
</tr>
<tr>
<td align="right"><b>Categoria:</b> </td>
<td><select name="vc_categoria" id="CATEGORIA#04" multiple size="4" style="background-color:#FBFBFB; border-style:groove">
<option value="1">Categoria valida</option>
<option value="2">Categoria válida tb</option>
</select></td>
</tr>
<tr bgcolor="#FBFBFB">
<td align="right"><b>E-Mail:</b> </td>
<td><input type="text" name="vc_email" style="background-color:#FBFBFB; border-style:groove" id="EMAIL#01"></td>
</tr>
<tr>
<td align="right"><b>CEP:</b> </td>
<td><input type="text" name="vc_cep" style="background-color:#FBFBFB; border-style:groove" id="CEP#06|9"></td>
</tr>
<tr bgcolor="#FBFBFB">
<td align="center" colspan="2"><input type="button" style="background-color:#FBFBFB; font-weight:bold; border-color:#999999; border-style:groove" name="bt_submeter" value="Enviar" onClick="valida_generico(this)"></td>
</tr>
</table>
</form>
<%
else
response.Write("FOI!")
end if
%>
</body>
</html>
testado em FF e IE.
