Check input value length

200,828

Solution 1

You can add a form onsubmit handler, something like:

<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>

Solution 2

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

if browser supports html5,

it will automatical be validate attributes(minlength) in tag

but Safari(iOS) doesn't working

Share:
200,828

Related videos on Youtube

user1609394
Author by

user1609394

Updated on May 28, 2020

Comments

  • user1609394
    user1609394 almost 4 years

    I have a problem with input checking. I don't want to send the request if the input length is less than 3.

    My form:

    <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>
    
    • Jezen Thomas
      Jezen Thomas over 11 years
      You've referenced testlenght(), but where is the function?
  • user1609394
    user1609394 over 11 years
    Can you please type full skript, i am not smart in javascript ;(
  • dm03514
    dm03514 over 11 years
    that should be the full thing
  • Fusseldieb
    Fusseldieb over 7 years
    I know this post is years old, but to help some beginners (Beginners should have a chance too, since we all began in similar ways.. sooo..) : The part of <form... to </form> should go in your plain html document and that whole function part into a <script> (put it here) </script> tag in that same document, or put it in a separate file called script.js and reference it with <script src='script.js'></script>. That's all. Sorry, if this comment isn't "S.O. compilant", I just want to help some bloody beginners.
  • vomako
    vomako over 7 years
    I think the "return" statement is missing: <form onsubmit="return validate();">