How to check input field's length?
<p>How am I able to check input field's length? </p>

<p>For example, how to check if name entered in <code>input</code> field has <code>length</code> bigger than <code>2</code> characters?</p>

<p>Here is the code that I tried to use, but <code>length</code> always returns <code>0</code>.</p>

<pre><code>$(document).ready(function () {
     name = $("#name").val();
     $("#name").change(function () {
         if (name.length &lt;= 2) {
             console.log(name.length);
             $("#name").addClass("error");
         } else if (name.length &gt; 2) {
             $("#name").removeClass("error");
         }
     });
 });
</code></pre>
