title: How to set a min and max character length on a HTML textbox using Javascript
body: <p>I am only GCSE level and I need a text box in my code to only have 4 characters. I have made it a max of 4 but I need it to have a minimum of 4 aswell. I also made it only accept numbers. I am not using HTML 5 at my school.</p>
  
  <p>This is my whole code:</p>
  
  <pre><code> &lt;head&gt;
      &lt;title&gt;Exam entry&lt;/title&gt;
      &lt;script language="javascript" type="text/javascript"&gt;
      function validateForm() {
      var result = true;
      var msg="";
  
  
  
  
  if (document.ExamEntry.name.value=="") {
  msg+="You must enter your name \n";
  document.ExamEntry.name.focus();
  document.getElementById('name').style.color="red";
  result = false;
  }
  if (document.ExamEntry.subject.value=="") {
  msg+="You must enter the subject. \n";
  document.ExamEntry.subject.focus();
  document.getElementById('subject').style.color="red";
  result = false;
  }
  if (document.ExamEntry.ExamNumber.value=="") {
  msg+="You must enter your exam number. \n";
  document.ExamEntry.ExamNumber.focus();
  document.getElementById('ExamNumber').style.color="red";
  result = false;
  }
  
  if(document.ExamEntry.ExamNoLen.value.length &lt;4) { 
  msg+="Your eaxm nuber must be 4 digits long.  \n";
  document.ExamEntry.ExamNumber.focus();
  document.getElementById('ExamNumber').style.color="red";
  result = false;
  }
  
  checkInput(elem){
      if(elem.value.length != 4){
          alert("This value needs to be 4 characters long!");
          elem.value = ""; // Reset the textbox
      }
      else{
          alert("This value is 4 characters long.");
      }
  }
  
  if(msg==""){
  return result;
  }
  {
  alert(msg)
  return result;
  }
  }
  &lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;h1&gt;Exam Entry Form&lt;/h1&gt;
  &lt;form name="ExamEntry" method="post" action="success.html"&gt;
  &lt;table width="50%" border="0"&gt;
  &lt;tr&gt;
  &lt;td id="name"&gt;Name&lt;/td&gt;
  &lt;td&gt;&lt;input type="text" name="name" /&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td id="subject"&gt;Subject&lt;/td&gt;
    &lt;td&gt;&lt;input type="text" name="subject" /&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
  &lt;td id="ExamNumber"&gt;Exam Number &lt;/td&gt;
  &lt;td&gt;
    &lt;input name="ExamNumber"  type="text"  
    onkeydown="return ( event.ctrlKey || event.altKey 
                      || (47&lt;event.keyCode &amp;&amp; event.keyCode&lt;58 &amp;&amp; event.shiftKey==false) 
                      || (95&lt;event.keyCode &amp;&amp; event.keyCode&lt;106)
                      || (event.keyCode==8) || (event.keyCode==9) 
                      || (event.keyCode&gt;34 &amp;&amp; event.keyCode&lt;40) 
                      || (event.keyCode==46) )" 
                      maxlength="4" onblur="checkInput(this)"  /&gt;
      &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
  &lt;td&gt;&lt;input type="submit" name="Submit" value="Submit" onClick="return validateForm();" /&gt;&lt;/td&gt;
  &lt;td&gt;&lt;input type="reset" name="Reset" value="Reset" /&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;/table&gt;
  &lt;/form&gt;
  &lt;/body&gt;
  </code></pre>