views:

32

answers:

4

I have this form with a JS function to verify that there are no blank fields, but it doesn't work.

<head>
  <script language='javascript'>
      function verifyForm(){
        var msg='';
        if(document.getElementById('field1').value==''){msg+='Field 1  \n';}
        if(document.getElementById('field2').value==''){msg+='Field 2  \n';}
        if(msg!=''){
            alert('The following entries are empty:\n\n'+msg);
            return false
        }else{
            return true }
        }
  </script>
</head>

<body>
   <form name="frmRequest2" id="frmRequest2" action="<? echo $submitURL2; ?>" method="post" onsubmit='return verifyForm();'>
   Please answer all questions and enter "NONE" where appropriate.

  <table>
    <tbody>
      <tr>
        <th>List any items you already have</th>
        <th>List any items you need to get</th>
      </tr>
      <tr>
        <td><textarea name="field1" id="field1" rows="2" cols="39"> <? echo $field1; ?> </textarea></td>
        <td><textarea name="field2" id="field2" rows="2" cols="39"> <? echo $field2; ?> </textarea></td>
      </tr>

      <tr>
        <td colspan="4" align="center" valign="top" style="border-width: 0px 0px 0px 0px;"> <br />
        <input name="submit" value="Submit Data" type="submit"/> </td>
      </tr>      
    </tbody>
  </table>
  </form>
</body>
+1  A: 

You're missing a ">" on your closing "[/head]" tag, you're not closing your "[form]" tag. That could cause a problem.

Eric Wendelin
The ">" and "</form>" are there in the original code. I lost them here in the example.
ChuckO
+4  A: 

You have extra closing curly braces (}) after your if clauses.

Edited to add: The following code works as expected and ignores any leading/trailing whitespace in the fields:

<html>

<head>
  <script language='javascript'>
      function verifyForm(){
        var msg='';
        if(document.getElementById('field1').value.replace(/\s+$|^\s+/, '') == '') {msg+='Field 1  \n';}
        if(document.getElementById('field2').value.replace(/\s+$|^\s+/, '') == '') {msg+='Field 2  \n';}
        if(msg!=''){
            alert('The following entries are empty:\n\n'+msg);
            return false;
        }else{
            return true;
        }
      }
  </script>
</head>

<body>
   <form name="frmRequest2" id="frmRequest2" action="" method="post" onsubmit='return verifyForm();'>
   Please answer all questions and enter "NONE" where appropriate.

   <table>
      <tbody>
      <tr>
        <th>List any items you already have</th>
        <th>List any items you need to get</th>
      </tr>
      <tr>
        <td><textarea name="field1" id="field1" rows="2" cols="39"></textarea></td>
        <td><textarea name="field2" id="field2" rows="2" cols="39"></textarea></td>
      </tr>

      <tr>
        <td colspan="4" align="center" valign="top" style="border-width: 0px 0px 0px 0px;">
          <input name="submit" value="Submit Data" type="submit"/>
        </td>
      </tr>
    </tbody>
  </table>

 </form>
</body>

</html>
Ryan Freebern
and that, too :)
Eric Wendelin
I removed the extra curly braces. It still does not work.
ChuckO
When I take that code and remove the curly braces, and fix the minor markup typos, it works fine. Use Firebug or something to make sure you don't have some other Javascript error.
Pointy
I went over my original code and found a missing id. Once I corrected that, the function worked. Thank you all.
ChuckO
+1  A: 

You have a space on each side of each of your default value. "" != " "

David Dorward
The extra spaces are not in the original code. I only added it to the example for clarity.
ChuckO
Thus providing a good example of why you should provide your code and not something that is merely like your code.
David Dorward
A: 

As a general rule, I like to trim the spaces of inputs. Does changing the if clause to

document.getElementById('field1').value == '' to
document.getElementById('field1').value.trim() == ''

Help at all?

Also, to reiterate what David suggested, your original code looks like

<textarea name="field1" id="field1" rows="2" cols="39"><? echo $field1; ?></textarea>

and not

<textarea name="field1" id="field1" rows="2" cols="39"> <? echo $field1; ?> </textarea>

Notice the spaces?

Haydar