jQuery & Handling Element ID's
<p>I have the following code:</p>

<pre><code>    function mandatoryField(manF)
    {
        var fieldId = $(manF).val();
        if(fieldId == "")
        {
            return false;
        }
        else
        {
            return true;
        }
    }
</code></pre>

<p>It doesn't work, but this does:</p>

<pre><code>    function mandatoryField()
    {
        var fieldId = $("#element_1").val();
        if(fieldId == "")
        {
            return false;
        }
        else
        {
            return true;
        }
    }
</code></pre>

<p>Presume, on my first example, mandatoryField is called as such:</p>

<pre><code>  mandatoryField("#element_1")
</code></pre>

<p>Why doesn't it work when I try to replace the absolute element ID name with a variable?</p>

<p><strong>Edit:</strong></p>

<p>Most recent code - non-working:</p>

<pre><code>    function isAmExSelected()
    {
         return $("#creditCardType").val() == "American Express";
    }

    function containsOnlyDigits(str)
    {
         return str.match(/[^0-9]/) == null;
    } 

    function validateCCNumber()
    {
         var ccn = $("#creditCardNumber").val();
         var onlyDigits = containsOnlyDigits(ccn);
         if(isAmExSelected())
         {
             return ccn.length == 15 &amp;&amp; onlyDigits;
         }
         else
         {
             return ccn.length == 16 &amp;&amp; onlyDigits;
         }
    }

    function mandatoryField(manF) 
    {
        var fieldId = $("#" + manF).val();
        return fieldId != "";
    }


    function registerValidation(id, validateMethod(), errorMethod)
    {
          $(id).change(function(){
              if(validateMethod() == false)
              {
                  errorMethod();
              }
          });
    }


    $(document).ready(function(){
        registerValidation("#creditCardNumber", validateCCNumber, function(){alert("Invalid Credit Card Number!")});
        $('input[type=text][class=mandatory]').blur(function(){
            if (mandatoryField(this.id)) {
                     alert('Field:' + this.id + ' is mandatory!')
            }
        });
    });
</code></pre>

<p><strong>Edit 2</strong></p>

<p>I've rewritten the entire thing to look like this:</p>

<pre><code>        $('input[type=text][class=mandatory]').blur(function(){
            if (!($("#" + this.id).val().length)) {
                     alert('Field:' + this.id + ' is mandatory!');
            }
        });
</code></pre>

<p>If a text input of the mandatory class blurs, then run the function: if #foo.val() does not have length (i.e. has no text in it), run the alert. I believe it should work, but it does not.</p>
