www.webdeveloper.com
Results 1 to 8 of 8

Thread: [RESOLVED] form validation question, "field1 is not defined error"

  1. #1
    Join Date
    Feb 2010
    Posts
    5

    resolved [RESOLVED] form validation question, "field1 is not defined error"

    Hi Everyone,

    I have this web form that I want to validate only four of the fields (these are just input text fields, one of which is for e-mail).

    The code is supposed to bring up an alert box when a fields is empty and after the user clicks ok puts the focus on the offending input field. For the e-mail box, it is supposed to also check for correct e-mail address form.

    While testing the first field, the alert text box appears, I click ok but the page sends the incomplete form to my processing page and causes and error. Firebug says that "field1 is not defined". Honestly it looks like it is defined to me.

    Can any of you take a look at the code below and see what I am doing wrong? Only my JS code appears below. I've searched online for possible solutions, but the things I've tried produced similar undefined errors. I admit I need more practice in JavaScript (familiar with JS, not proficient), and of course I could use help in a hurry to finish this project...

    Thanks in advance for your help!

    kc2010


    PHP Code:
    <script type="text/javascript">
    <!--
    function 
    validate_required(field,alerttxt)
    {
    with (field)
      {
        if (
    field.value==null || field.value=="")
          {
          
    alert(alerttxt);return false;
          }
        else
          {
          return 
    true;
          }
      }
    }

    function 
    validate_email(field,alerttxt)
    {
    with (field)
      {
      
    apos=value.indexOf("@");
      
    dotpos=value.lastIndexOf(".");
      if (
    apos<1||dotpos-apos<2)
        {
    alert(alerttxt);return false;}
      else {return 
    true;}
      }
    }

    function 
    validate_form(thisform)
    {
    with (thisform)
      {
        if (
    validate_required("field1","Please fill in your first name to continue.")==false)
        {
    field1.focus();return false;}
      
        else if (
    validate_required("field2","Please fill in your last name to continue.")==false)
        {
    field2.focus();return false;}
      
        else if (
    validate_required("field12","Please enter your country to continue.")==false)
        {
    field12.focus();return false;}
      
        else if (
    validate_required("field3","Please enter your e-mail address to continue.")==false)
        {
    field3.focus();return false;}
      
        else if (
    validate_email("field3","Please enter a valid e-mail address to continue.")==false)
        {
    field3.focus();return false;}
      
        else {return 
    true;}
      }
    }

    // -->
    </script> 

  2. #2
    Join Date
    Jun 2007
    Posts
    667
    Code:
    if (validate_required("field1",
    You're passing a string but validate_required expects a reference.
    also the with{} block in validate_required is not needed.
    Y_U U_G_A_E_U_ B_S_A_D_

  3. #3
    Join Date
    Feb 2010
    Posts
    5
    Thanks SterlingIsFine,

    I initially did not have the quotes around the field1, etc., this happened to be the only way I found to get the alert box to appear.

    I made those changes and now no alert boxes appear when I tested- it goes straight to my processing page. Firebug doesn't give me errors now.

    What else is wrong with this code? Revised code appears below...

    TIA!


    PHP Code:
    <script type="text/javascript">
    <!--
    function 
    validate_required(field,alerttxt)
    {
      if (
    field.value==null || field.value=="")
        {
        
    alert(alerttxt);return false;
        }
      else
        {
        return 
    true;
        }
    }

    function 
    validate_email(field,alerttxt)
    {
    with (field)
      {
      
    apos=value.indexOf("@");
      
    dotpos=value.lastIndexOf(".");
      if (
    apos<1||dotpos-apos<2)
        {
    alert(alerttxt);return false;}
      else {return 
    true;}
      }
    }

    function 
    validate_form(thisform)
    {
    with (thisform)
      {
        if (
    validate_required(field1,"Please fill in your first name to continue.")==false)
        {
    field1.focus();return false;}
      
        else if (
    validate_required(field2,"Please fill in your last name to continue.")==false)
        {
    field2.focus();return false;}
      
        else if (
    validate_required(field12,"Please enter your country to continue.")==false)
        {
    field12.focus();return false;}
      
        else if (
    validate_required(field3,"Please enter your e-mail address to continue.")==false)
        {
    field3.focus();return false;}
      
        else if (
    validate_email(field3,"Please enter a valid e-mail address to continue.")==false)
        {
    field3.focus();return false;}
      
        else {return 
    true;}
      }
    }
    // -->

    </script> 

  4. #4
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    Code:
        if (validate_required("field1","Please fill in your first name to continue.")==false)
        {thisform.field1.focus();return false;}
    At least 98% of internet users' DNA is identical to that of chimpanzees

  5. #5
    Join Date
    Feb 2010
    Posts
    5
    Thanks Fang.

    With the addition of thisform. as you suggested, I still get the "field1 is not defined" error message which points to the this line if (validate_required(field1,"Please fill in your first name to continue.")==false), as shown below.


    PHP Code:
    function validate_form(thisform)
    {
    with (thisform)
      {
        if (
    validate_required(field1,"Please fill in your first name to continue.")==false)
        {
    thisform.field1.focus();return false;} 
    validate_form doesn't seem to know that field1 is defined from the form. Here is a bit more code from the actual form, just for field1...

    PHP Code:
    <li id="fo1li1" class="    ">
    <
    label class="desc" id="title1" for="Field1">
    Name
    <span id="req_1" class="req">*</span>
    </
    label>

    <
    span>
    <
    input id="Field1" name="Field1" type="text" class="field text" value="" size="20" tabindex="1"  />
    <
    label for="Field1">First</label>
    </
    span
    Not sure if this will help any of you in helping me, but I cannot see what is going on...(unfortunately, I've been staring at this code for over a couple of days). Hopefully something will jump out in this code for one of you.

    Thanks again.

  6. #6
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    QUOTED!
    Code:
        if (validate_required("field1","Please fill in your first name to continue.")==false)
        {thisform.field1.focus();return false;}
    At least 98% of internet users' DNA is identical to that of chimpanzees

  7. #7
    Join Date
    Feb 2010
    Posts
    5
    Thanks Fangs... I did remove the quotes from there but the error message remains the same... field1 is not defined

    Is there anything else?

    TIA!

  8. #8
    Join Date
    Feb 2010
    Posts
    5

    Error Resolved

    Thanks to everyone who tried to help.

    kc2010

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles