Texto fora do lugar

4 respostas
airmigjr

Olá pessoal, como vão?
Preciso de uma ajuda aqui, pois o meu código aparentemente está correto, mas o texto que resulta de uma mensagem de erro que deveria estar aparecendo num determinado

não está aparecendo onde deveria. O loop funciona, pois quando executo o código as mensagens são as devidas, porém fora de lugar. Segue código:

<html>
<head>
<title>My form</title>
<script type = "text/javascript">

function register(){

	var x = new Array();
	x[0] = document.getElementById('name').value;
	x[1] = document.getElementById('lname').value;
	x[2] = document.getElementById('email').value;
	x[3] = document.getElementById('password').value;
	x[4] = document.getElementById('cpassword').value;
	
	
	var h = new Array();
	
	h[0] = "<span style = 'color:red;'>Please type a name!</span>";
	h[1] = "<span style = 'color:red;'>You must type a last name!</span>";
	h[2] = "<span style = 'color:red;'>You must type a valid email!</span>";
	h[3] = "<span style = 'color:red;'>You must type a password!</span>";
	h[4] = "<span style = 'color:red;'>You must confirm a password!</span>";
	
	
	
	var divs = new Array("mname","mlname","memail","mpassword","mcpassword");
	
	for (i in x){
	var error = h[i];
	var div = divs[i];
	
	if(x[i]==""){
	document.getElementById(div).innerHTML = error;
	}else{
		document.getElementById(div).innerHTML = "OK!";
		}
				}
	
	
	}


</script>


</head>

<body>
<h3>My Form</h3>
	<form>
		<table border="1">
			<tr><td>Name:</td><td><input type="text" id="name" /></td><td><div id = "mname"></div></td></tr>
			<tr><td>Last Name:</td><td><input type="text" id="lname" /></td><div id = "mlname"></div></td></tr>
			<tr><td>Email:</td><td><input type="text" id="email" /></td><div id = "memail"></div></td></tr>
			<tr><td>Password:</td><td><input type="text" id="password" /></td><div id = "mpassword"></div></td></tr>
			<tr><td>Confirm Password:</td><td><input type="text" id="cpassword" /></td><div id = "mcpassword"></div></td></tr>
	</form>
		</table><br />
	<input type="button" value="submit" onclick="register()" />


</body>
</html>
[img]http://s17.postimage.org/iddgjig7z/Screen_Shot_2012_07_02_at_11_26_24_AM.png[/img]

4 Respostas

al.barbosa

airmigjr,

Faltaram algumas tags

na tabela, antes das tags div, a partir do Last Name:
<table border="1">  
            <tr><td>Name:</td><td><input type="text" id="name" /></td><td><div id = "mname"></div></td></tr>  
            <tr><td>Last Name:</td><td><input type="text" id="lname" /></td><td><div id = "mlname"></div></td></tr>  
            <tr><td>Email:</td><td><input type="text" id="email" /></td><td><div id = "memail"></div></td></tr>  
            <tr><td>Password:</td><td><input type="text" id="password" /></td><td><div id = "mpassword"></div></td></tr>  
            <tr><td>Confirm Password:</td><td><input type="text" id="cpassword" /></td><td><div id = "mcpassword"></div></td></tr>
RiQuInHo_
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Strict//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>

<title>My form</title>
<script type="text/javascript">   
  
function register(){   
  
    var x = new Array();   
    x[0] = document.getElementById('name').value;   
    x[1] = document.getElementById('lname').value;   
    x[2] = document.getElementById('email').value;   
    x[3] = document.getElementById('password').value;   
    x[4] = document.getElementById('cpassword').value;   
       
       
    var h = new Array();   
       
    h[0] = "<span style = 'color:red;'>Please type a name!</span>";   
    h[1] = "<span style = 'color:red;'>You must type a last name!</span>";   
    h[2] = "<span style = 'color:red;'>You must type a valid email!</span>";   
    h[3] = "<span style = 'color:red;'>You must type a password!</span>";   
    h[4] = "<span style = 'color:red;'>You must confirm a password!</span>";   
       
       
       
    var divs = new Array("mname","mlname","memail","mpassword","mcpassword");   
       
    for (i in x){   
    var error = h[i];   
    var div = divs[i];   
       
    if(x[i]==""){   
    document.getElementById(div).innerHTML = error;   
    }else{   
        document.getElementById(div).innerHTML = "OK!";   
        }   
                }   
       
       
    }   
  
  
</script>

</head>

<body>
	<h3>My Form</h3>
	<table border="1">
		<tr>
			<td>Name:</td>
			<td><input type="text" id="name" /></td>
			<td><div id="mname"></div></td>
		</tr>
		<tr>
			<td>Last Name:</td>
			<td><input type="text" id="lname" /></td>
			<td><div id="mlname"></div></td>
		</tr>
		<tr>
			<td>Email:</td>
			<td><input type="text" id="email" /></td>
			<td><div id="memail"></div></td>
		</tr>
		<tr>
			<td>Password:</td>
			<td><input type="text" id="password" /></td>
			<td><div id="mpassword"></div></td>
		</tr>

		<tr>
			<td>Confirm Password:</td>
			<td><input type="text" id="cpassword" /></td>
			<td><div id="mcpassword"></div></td>
		</tr>

	</table>
	<br />
	<input type="button" value="submit" onclick="register()" />

</body>
</html>
airmigjr

Muito Obrigado! Vc conhece algum bom IDE para a programação em JScript? Estou fazendo isso no Notepad++ …

RiQuInHo_

hahah cara eu gosto do ide do Eclipse ai vai de voce tbm…

Criado 2 de julho de 2012
Ultima resposta 2 de jul. de 2012
Respostas 4
Participantes 3