title: javascript: counting digits in a text input
body: <p>I have a piece of HTML that creates a web form with three text fields (name, group and number), all of which are validated using JavaScript to check that there is data inputted into them. In the last text field, I need to introduce an additional bit of JavaScript to check that the data inputted by the user is also four digits long (for example 2947 or 94Q3). As a complete JavaScript novice, I'm not sure how I would do this! Would I have to create a variable that could take the value of the inputted data, then count the digits of the variable, or could I do it directly from the field? Here is the Javascript section of my code:</p>
  
  <pre><code>function validateForm() {
      var result = true;
      var msg = ””;
      if (document.Entry.name.value == ””) {
          msg += ”You must enter your name\n”;
          document.Entry.name.focus();
          document.getElementById(‘name’).style.color = ”red”;
          result = false;
  
          if (document.Entry.group.value == ””) {
              msg += ”You must enter the group\n”;
              document.Entry.group.focus();
              document.getElementById(‘group’).style.color = ”red”;
              result = false;
          }
  
          if (document.Entry.number.value == ””) {
              msg += ”You must enter the number\n”;
              document.Entry.number.focus();
              document.getElementById(‘number’).style.color = ”red”;
              result = false;
          }
  
          if (msg == ””) {
              return result;
          } {
              alert(msg)
              return result;
          }
      }
  </code></pre>
  
  <p>If possible, could you tell me what code I would need to insert? Thank you!</p>