Skip to content

Instantly share code, notes, and snippets.

@crystianwendel
Created October 18, 2013 13:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crystianwendel/7041779 to your computer and use it in GitHub Desktop.
Save crystianwendel/7041779 to your computer and use it in GitHub Desktop.
Textarea com contagem de caracteres
<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