PDA

Visualizza versione completa : [JSP] Limitare testo in una textarea


Alhazred
21-07-2008, 21.08.26
Ho una JSP il cui codice è questo:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>

</head>
<body>
<center>
<h2>Crea Ricetta</h2><br><br>
Preparazione ricetta
<form>
<table width="400" border="0" cellpadding="0" cellspacing="0">
<!--DWLayoutTable-->
<tr>
<td width="auto" height="275">
<table width="100%" border="0" cellpadding="1" cellspacing="5">
<tr>
<td colspan="2"><textarea name="preparazione" rows="6" cols="50"></textarea>
</td>
</tr>
<tr>
<td>Nome ricetta</td>
<td><input type="text" name="nome" maxlength="40"/></td>
</tr>
<tr>
<td>Tipo di portata</td>
<td><select name="tipo" size="1">
<option value="primo">Primo</option>
<option value="secondo">Secondo</option>
<option value="dolce">Dolce</option>
</select>
</td>
</tr>
<tr>
<td>Difficoltà</td>
<td><select name="tipo" size="1">
<option value="facile">Facile</option>
<option value="media">Media</option>
<option value="difficile">Difficile</option>
</select>
</td>
</tr>
<tr>
<td>Tempo di preparazione</td>
<td><input type="text" name="tempo" size="5"/> minuti</td>
</tr>
<tr>
<td>Ingredienti Principali</td>
<td><select name="Ingrediente1" size="1">
<option value="facile">Merluzzo</option>
<option value="media">Pomodoro</option>
<option value="difficile">Fusilli</option>
</select>
<select name="Ingrediente2" size="1">
<option value="facile">Merluzzo</option>
<option value="media">Pomodoro</option>
<option value="difficile">Fusilli</option>
</select>
<select name="Ingrediente3" size="1">
<option value="facile">Merluzzo</option>
<option value="media">Pomodoro</option>
<option value="difficile">Fusilli</option>
</select>
</td>
</tr>
<tr><td>&nbsp;</td><td>&nbsp;</td></tr>
<tr>
<td></td>
<td><input type="submit" name="submit" value="Inserisci ricetta" /></td>
</tr>
</table></td>
</tr>
</table>
</form>
<br>
<a href="errorericetta.html">Errore</a>
</center>
</body>
</html>

Vorrei che nella textarea sia possibile inserire massimo 1000 caratteri ma non ci riesco. Ho provato alcune soluzioni trovate cercando con google, ma non mi funzionano.
Come posso fare?

miciomao
22-07-2008, 10.14.10
é un po datato ma funziona ancora




<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Limitare caratteri in una textarea</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">

var ns6=document.getElementById&&!document.all

function restrictinput(maxlength,e,placeholder){
if (window.event&&event.srcElement.value.length>=maxlength)
return false
else if (e.target&&e.target==eval(placeholder)&&e.target.value.length>=maxlength){
var pressedkey=/[a-zA-Z0-9\.\,\/]/ //detect alphanumeric keys
if (pressedkey.test(String.fromCharCode(e.which)))
e.stopPropagation()
}
}

function countlimit(maxlength,e,placeholder){
var theform=eval(placeholder)
var lengthleft=maxlength-theform.value.length
var placeholderobj=document.all? document.all[placeholder] : document.getElementById(placeholder)
if (window.event||e.target&&e.target==eval(placeholder)){
if (lengthleft<0)
theform.value=theform.value.substring(0,maxlength)
placeholderobj.innerHTML=lengthleft
}
}


function displaylimit(thename, theid, thelimit){
var theform=theid!=""? document.getElementById(theid) : thename
var limit_text='<b>Puoi inserire ancora <span id="'+theform.toString()+'">'+thelimit+'</span> caratteri</b>'
if (document.all||ns6)
document.write(limit_text)
if (document.all){
eval(theform).onkeypress=function(){ return restrictinput(thelimit,event,theform)}
eval(theform).onkeyup=function(){ countlimit(thelimit,event,theform)}
}
else if (ns6){
document.body.addEventListener('keypress', function(event) { restrictinput(thelimit,event,theform) }, true);
document.body.addEventListener('keyup', function(event) { countlimit(thelimit,event,theform) }, true);
}
}
</script>
</head>
<body>
<h1>Limitare i caratteri in una textarea</h1>

<form name="modulo">
<p>
<textarea name="commenti" id="commenti" cols=25 rows=15></textarea><br>

<script>
displaylimit("","commenti",50)
</script>
</form>
</body>
</html>

Alhazred
22-07-2008, 10.36.11
Ho risolto con questa funzione javascript

function textCounter(field, countfield, maxlimit) {
if (field.value.length > maxlimit) // se sono stati scritti troppi caratteri
field.value = field.value.substring(0, maxlimit); //li taglia.
// altrimenti aggiorna il contatore
else
countfield.value = maxlimit - field.value.length;
}

Chiamandola così

onKeyDown="textCounter(this.form.descrizione, this.form.remLen, 1000);" onKeyUp="textCounter(this.form.descrizione, this.form.remLen, 1000);"

e questo è il contatore

<input readonly type=text name=remLen size=3 maxlength=4 value="1000">