je souhaite limiter le nombre de caracteres à inscrir dans un textarea
est ce possible?
merci d'avance
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Limite le nombre de caractères d'un textarea</title>
<script type="text/javascript">
/* <![CDATA[ */
function limite(zone)
{
var max = 20;
if(zone.value.length >= max)
{
zone.value = zone.value.substring(0,max);
}
var reste = max - zone.value.length;
var affichage_reste = 'Vous pouvez encore inscrire '+ reste +' caractères';
document.getElementById('max_desc').innerHTML = affichage_reste;
}
/* ]]> */
</script>
</head><body>
<form name="menu1">
<textarea name="zone2Text" cols="40" rows="10" onfocus="this.value='';" onkeyup="limite(this);" onkeydown="limite(this);">Votre texte ici.</textarea><br />
<span id="max_desc"></span>
</form>
</body>
</html>
La page est complète en soi, teste-là et inspire toi pour faire ton propre code Code : Tout sélectionner
onLoad="limite(this);"<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Limite le nombre de caractères d'un textarea</title>
<script type="text/javascript">
/* <![CDATA[ */
function limite(zone)
{
var max = 20;
if(zone.value.length >= max)
{
zone.value = zone.value.substring(0,max);
}
var reste = max - zone.value.length;
document.getElementById('max_desc').innerHTML = reste;
}
/* ]]> */
</script>
</head><body>
<form name="menu1">
<textarea name="zone2Text" cols="40" rows="10" onfocus="this.value='';" onkeyup="limite(this);" onkeydown="limite(this);">Votre texte ici.</textarea><br />
<p>vous pouvez encore inscrire <span id="max_desc">20</span> caractères</p>
</form>
</body>
</html>
encore merci à toi cyrano Code : Tout sélectionner
<link href="js/limite_txt.js" type="text/javascript" /> Code : Tout sélectionner
<script type="text/javascript" src="./mes_scripts/mon_fichier.js"></script>Code : Tout sélectionner
<textarea>Code : Tout sélectionner
onfocus="this.value='';"Code : Tout sélectionner
<html>
<head>
<title></title>
<script type="text/javascript">
/* <![CDATA[ */
function limite(zone)
{
var max = 500;
if(zone.value.length >= max)
{
zone.value = zone.value.substring(0,max);
}
var reste = max - zone.value.length;
var affichage_reste = '<font face="comic sans ms" size="2">Limite : 500. Il vous reste '+ reste +' caractères.</font>';
document.getElementById('max_desc').innerHTML = affichage_reste;
}
/* ]]> */
</script>
</head>
<body>
<textarea name="message" cols="110" rows="11" style="font-family: comic sans ms; font-size: 10pt" onfocus="this.value='';" onkeyup="limite(this);" onkeydown="limite(this);"></textarea>
<br>
<span id="max_desc"></span>
</body>
</html>