Visualizzazione dei risultati da 1 a 8 su 8
  1. #1
    Utente di HTML.it L'avatar di [trodat]
    Registrato dal
    Oct 2004
    Messaggi
    2,135

    Minimo 10 caratteri in textarea

    Ciao.

    Utilizzo questa funzione javascript per controllare il numero di caratteri inseriti in una textarea che non devono essere superiori a 255.

    codice:
    function LunghezzaMax(commento) 
    { 
    if (commento.value.length > 255) 
     { 
    alert("Inserire nel campo \"\Commento\"\nal massimo 255 caratteri."); 
    commento.value = commento.value.substring(0, 255); 
     } 
    }
    
    function textCounter(field, countfield, maxlimit) {
    if (field.value.length > maxlimit) 
    field.value = field.value.substring(0, maxlimit);
    else 
    countfield.value = maxlimit - field.value.length;
    }
    
    ....
    
    
    <textarea name="commento" rows="5" cols="32" 
    onChange="LunghezzaMax(this)"; 
    onKeyDown="textCounter(this.form.commento,this.form.remLen1,255);" onKeyUp="textCounter(this.form.commento,this.form.remLen1,255);">
    </textarea>
    Adesso però devo anche controllare che almeno ci siano 10 caratteri inseriti nella textarea, cioè l'intervallo diventa minimo 10/massimo 255 caratteri.

    Cosa devo modificare?
    Grazie
    Lo Stato dà un posto. L’impresa privata dà un lavoro. – Indro Montanelli

  2. #2
    prova così:
    codice:
    if ((commento.value.length > 255)&&(commento.value.length<10))
    {
    //
    }
    oppure se vuoi alert differenti:
    codice:
    if (commento.value.length > 255)
    {
    //primo alert
    }
    if(commento.value.length<10)
    {
    //secondo alert
    }

  3. #3
    Utente di HTML.it L'avatar di [trodat]
    Registrato dal
    Oct 2004
    Messaggi
    2,135
    grazie!
    Lo Stato dà un posto. L’impresa privata dà un lavoro. – Indro Montanelli

  4. #4
    Utente di HTML.it L'avatar di [trodat]
    Registrato dal
    Oct 2004
    Messaggi
    2,135
    Originariamente inviato da E.d.i.73
    prova così:
    codice:
    if ((commento.value.length > 255)&&(commento.value.length<10))
    {
    //
    }
    oppure se vuoi alert differenti:
    codice:
    if (commento.value.length > 255)
    {
    //primo alert
    }
    if(commento.value.length<10)
    {
    //secondo alert
    }
    Scusa ma adesso che l'ho provato non va... il form parte anche se inserisco un solo carattere nella textarea...
    Lo Stato dà un posto. L’impresa privata dà un lavoro. – Indro Montanelli

  5. #5
    Postami il codice come l'hai modificato..

  6. #6
    Utente di HTML.it L'avatar di [trodat]
    Registrato dal
    Oct 2004
    Messaggi
    2,135
    OK, ecco:
    codice:
    <script language="javascript" type="text/javascript">
    <!--
    
    function LunghezzaMax(commento) 
    { 
    if (commento.value.length > 255 && commento.value.length < 10)
     { 
    alert("Inserire nel campo \"\Commento\"\nminimo 10 caratteri ed al massimo 255 caratteri."); 
    commento.value = commento.value.substring(0, 255); 
     } 
    }
    
    function textCounter(field, countfield, maxlimit) {
    if (field.value.length > maxlimit) 
    field.value = field.value.substring(0, maxlimit);
    else 
    countfield.value = maxlimit - field.value.length;
    }
    
    //-->
    </script>
    
    
    .......
    
    
    <textarea name="commento" rows="5" cols="32" 
    onChange="LunghezzaMax(this)"; 
    onKeyDown="textCounter(this.form.commento,this.form.remLen1,255);" onKeyUp="textCounter(this.form.commento,this.form.remLen1,255);">
    </textarea>
    Se scrivo così:
    codice:
    if ((commento.value.length > 255)&&(commento.value.length<10))
    La pagina va in errore: previsto oggetto.

    Grazie-
    Lo Stato dà un posto. L’impresa privata dà un lavoro. – Indro Montanelli

  7. #7
    codice:
     <script  type="text/javascript">
    
    
    function LunghezzaMax(commento)
    {
    if (commento.value.length > 255 || commento.value.length < 10)
     {
    alert("Inserire nel campo \"\Commento\"\nminimo 10 caratteri ed al massimo 255 caratteri.");
    
     }
    }
    
    
    </script>
    
    
    
    
    
    <textarea name="commento" rows="5" cols="32" onchange="LunghezzaMax(this)">
    </textarea>
    questo controllo all'evento onchange adesso è corretto,per l'altra parte del codice:

    onKeyDown="textCounter(this.form.commento,this.for m.remLen1,255);" onKeyUp="textCounter(this.form.commento,this.form. remLen1,255);

    remLen1 a che cosa è riferito?

  8. #8
    Utente di HTML.it L'avatar di [trodat]
    Registrato dal
    Oct 2004
    Messaggi
    2,135
    Grazie, adesso funziona:

    codice:
    <html>
    
    <head>
    <script language="javascript" type="text/javascript">
    <!--
    
    function LunghezzaMax(commento)
    {
    if (commento.value.length > 255 || commento.value.length < 10)
     {
    alert("Inserire nel campo \"\Commento\"\nminimo 10 caratteri ed al massimo 255 caratteri.");
    
     }
    }
    
    
    function textCounter(field, countfield, maxlimit) {
    if (field.value.length > maxlimit) 
    field.value = field.value.substring(0, maxlimit);
    else 
    countfield.value = maxlimit - field.value.length;
    }
    
    //-->
    </script>
    
    
    </head>
    
    <body>
    
    <form method="POST" action="">
    	
    
    
    	
    <textarea name="commento" rows="5" cols="32" onchange="LunghezzaMax(this)"; onKeyDown="textCounter(this.form.commento,this.form.remLen1,255);" onKeyUp="textCounter(this.form.commento,this.form.remLen1,255);">
    </textarea>	
    
    <input readonly type=text name=remLen1 size=3 maxlength=3 value="255">
    	
    	<input type="submit" value="Invia" name="B1">
    
    </form>
    
    </body>
    
    </html>
    Lo Stato dà un posto. L’impresa privata dà un lavoro. – Indro Montanelli

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.