2012-08-28 17 views
7

Ho un problema con il controllo dell'input. Non voglio per inviare la richiesta se la lunghezza di ingresso è inferiore a 3.Controllare la lunghezza del valore di input

La mia forma:

<form method='post' action=''> 
    Albūma nosaukums: # # this is the input --><input id='titleeee' type='text' name'album_title' /><br /> 

    Bilde Nr 1: <input type='file' name='pic_nr1' /><br /> 
    Bilde Nr 2: <input type='file' name='pic_nr2' /><br /> 
    Bilde Nr 3: <input type='file' name='pic_nr2' /><br /> 

    Aktīvs*: 
    <select> 
     <option>Jā</option> 
     <option>Nē</option> 
    </select> 

    <br /> 

    <input Onclick='testlenght(document.getElementById("titleeee"), "Tavs albūma nosaukums ir pa īsu!", "3")' type='submit' value='Pievienot' /> 
</form> 
+0

Hai fatto riferimento 'testlenght()', ma dove è la funzione? –

risposta

18

È possibile aggiungere un modulo gestore onsubmit, qualcosa di simile:

<form onsubmit="return validate();"> 

</form> 


<script>function validate() { 
// check if input is bigger than 3 
var value = document.getElementById('titleeee').value; 
if (value.length < 3) { 
    return false; // keep form from submitting 
} 

// else form is good let it submit, of course you will 
// probably want to alert the user WHAT went wrong. 

return true; 
}</script> 
+0

Puoi digitare full skript, non sono intelligente in javascript; ( – user1609394

+0

che dovrebbe essere la cosa completa – dm03514

+16

@ user1609394 - Questo è un sito di domande e risposte, non un sito "fai la mia codifica per me" .Se non sei intelligente con JS, impara! – PitaJ

2

<input type='text' minlength=3 /><br />

se il browser supporta HTML5,

si auto matico essere convalidare attributi (MINLENGTH) nel tag

ma Safari (iOS) non lavora

Problemi correlati