[Javascript] Integer and String

Flavio Gomes flavio at economisa.com.br
Mon Apr 26 09:12:26 CDT 2004


People, I'd like an opnion about the loop I created:

I pass "form" (an actual form object) as an argument to a function that loops over this form's inputs and validates them:


//=============================
function validateForm(objForm)
{
  error_message = "";
  focus_id      = "";

  for (x = 0; objForm[x]; x++)
  {
    name = (objForm[x].name)?objForm[x].name:"";
    switch (name)
    {
      case "id_number":
      if(!objForm[x].value)  //if value wasnt informed
      {
        error_message += "* \"ID Number\" must be informed\n";
        if(!focus_id) { focus_id = x; } //if "focus_id" isnt set, then store it
      }
      break;
      
      case "member_name":
      if(!objForm[x].value)  //if value wasnt informed
      {
        error_message += "* \"Member Name\" must be informed\n";
        if(!focus_id) { focus_id = x; } //if "focus_id" isnt set, then store it
      }
      break;
   }
  }

  if(error_message != "")
  {
    alert(error_message);
    objForm[focus_id].focus(); //Focus the input whose focus_id was stored
    return false;
  }
}
//=============================



Now you ask me: "Ok, and what's the problem":

  In the line where I commented "if(!focus_id) { focus_id = x; }" the "x" for "id_number" is "0" and even if I store it, on the next check "if(!focus_id)" it returns true and set the "focus_id" to the next input...
  The nearest to solution that I found was this:  "if(focus_id.toString() != parseInt(focus_id));", and that's what I'd like to hear suggestions on.

Thanks in advance,

--
Flavio Gomes
flavio at economisa.com.br
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.evolt.org/pipermail/javascript/attachments/20040426/8e0756e5/attachment.htm>


More information about the Javascript mailing list