Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Jul 2005
    Posts
    41
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Trouble making error message

    Trouble with a script. I want an error message in red to print to the top of the form if the user didn't fill out two of the fields but it's not working - it doesn't stop like its supposed to, it still submits the form. Here's the part of my code I know has problems:

    Code:
    function CheckTheForm() {
        var result = true;
        var msg="";
        
      if (document.frmTest.name.value=="" OR document.frmTest.eml.value=="") {
         newHTML = "<span style=\"color:red\"> Please fill out all required fields.</span>";
    document.getElementById('errortxt').innerHTML =newHTML;
            result = false;
    }
    
    return result;
    And then where I want it to print out:

    Code:
    <form name="frmTest" method="post" action="test_form.html">
    <p id="errortxt"></p>
    <table width="21%" border="0">
      <tr>
        <td id="title">Name</td>
        <td><input type="text" name="name" /></td>
      </tr>
      <tr>
        <td id="eml">email</td>
        <td><input type="text" name="eml" /></td>
      </tr>
      <tr>
        <td><input type="submit" name="Submit" value="Submit" onclick="return CheckTheForm();" /></td>
        <td><input type="reset" name="Reset" value="Reset" /></td>
      </tr>
    </table>
    Any ideas?

  • #2
    Banned
    Join Date
    May 2006
    Location
    England
    Posts
    664
    Thanks
    0
    Thanked 84 Times in 84 Posts
    1) There is no OR operator in JS. The logical OR operator is ||.

    2) Don't name a form element name or any other word that matches any form property.

    3) Always check the error console.

  • #3
    New Coder
    Join Date
    Jul 2005
    Posts
    41
    Thanks
    0
    Thanked 0 Times in 0 Posts

    thanks

    Thanks Arty Effem! that worked splendidly.


  •  

    Posting Permissions

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