0

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>
3
  • What does, "it doesn't work" mean? Commented Jul 9, 2010 at 14:20
  • The form is submitted even if the fields are empty. Commented Jul 9, 2010 at 14:37
  • The alert that you coded into your validation routine. (The point is moot anyway because I've verified that by fixing things mentioned in the answers below, it works fine.) Commented Jul 9, 2010 at 14:51

4 Answers 4

4

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>
Sign up to request clarification or add additional context in comments.

2 Comments

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.
I went over my original code and found a missing id. Once I corrected that, the function worked. Thank you all.
1

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

1 Comment

The ">" and "</form>" are there in the original code. I lost them here in the example.
1

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

2 Comments

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

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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.