Created
October 18, 2013 13:47
-
-
Save crystianwendel/7041779 to your computer and use it in GitHub Desktop.
Textarea com contagem de caracteres
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Titulo</title> | |
<link rel="stylesheet" type="text/css" href="estilo.css"/> | |
<style rel="stylesheet" type="text/css"> | |
.red { | |
color: red; | |
} | |
</style> | |
</head> | |
<body> | |
<form action="http://www.headfirstlabs.com/contest.php" method="POST" | |
onsubmit="return valida();"> | |
<p>Primeiro nome: <input id="firstname" type="text" name="firstname"/></p> | |
<p>Último nome: <input id="lastname" type="text" name="lastname"/></p> | |
<p> | |
Mensagem: | |
<textarea cols="80" rows="10" id="msg" name="msg" | |
onkeyup="conta_caracteres()"> | |
</textarea><br/> | |
(<span id="txt_info"></span>) | |
</p> | |
<input type="submit" value="enviar"/> | |
</form> | |
<script language="javascript"> | |
function conta_caracteres() { | |
txt = document.getElementById('msg'); | |
txt_info = document.getElementById('txt_info'); | |
if (txt.value.length > 200) { | |
txt.value = txt.value.substring(0, 200); | |
} else { | |
txt_info.innerHTML = (200 - txt.value.length) + " caracteres restantes"; | |
txt_info.className = ""; | |
} | |
} | |
function valida() { | |
txt = document.getElementById('msg'); | |
if (txt.value.length > 200) | |
return false; | |
return true; | |
} | |
</script> | |
</body> | |
</html> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment