form mandatory fields

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Dameon99
    New Member
    • Jun 2007
    • 57

    #1

    form mandatory fields

    Ive been looking through a whole tonne of sites to try to find out how I can make my fields mandatory but havnt been able to find something so far which actually tells me whwta I want to know. Ive tried a couple of code segments but they havnt been working. can anyone tell me how I can make fields in my code mandatory?

    My attempt at solving this is below:

    Code:
    <html>
    <head>
    
    <title>Coals Spiral Brochure Enquiry Form</title>
    
    <meta name="description" content="">
    
    <meta name="keywords" content="">
    <script language="javascript" 
      type="text/javascript">
    
    function validateForm(brochure1)
    {
    
    if(""==document.forms.brochure1.firstname.value)
    {
    alert("Please enter your first name.");
    return false;
    }
    
    if(""==document.forms.brochure1.lastname.value)
    {
    alert("Please enter your last name.");
    return false;
    }
    
    if(""==document.forms.brochure1.email.value)
    {
    alert("Please enter your email address.");
    return false;
    }
    
    }
    if(""==document.forms.brochure1.phone.value)
    {
    alert("Please enter your Phone number.");
    return false;
    }
    
    }
    
    if(""==document.forms.brochure1.country.value)
    {
    alert("Please enter your country.");
    return false;
    }
    
    }
    
    </script>
    </head>
    <body bgcolor="white" text="black">
    
    <table width="60%" align="center">
    <tr>
    <td>
    <font face=Arial size=2>
    
    <IMG SRC="http://www.mdmintec.reachpoint.com/Data/mdmintec/Roche%20MT%20sm.jpg" align="right">
    <strong><BR><BR><BR><BR><BR>
    Coals Spiral Brochure Enquiry Form</strong>
    <HR style="WIDTH: 100%; HEIGHT: 1px" align=center width="100%" color=black>
    <BR>
    Please fill out your details below in order to receive our Coals Spiral Brochure.
    <BR><BR>
    
    <form method="post"  onsubmit='return validateForm()' action="http://svr86.ehostpros.com/~minera86/brochure1.php">
    <TABLE width=85% align=center cellSpacing=1 cellPadding=1 border=0>
    <TR>
    <TD>
    <font face=Arial size=2>First Name: 
    </TD>
    <TD>
    <INPUT title="Your Google Toolbar can fill this in for you. Select AutoFill" maxLength=100 size=40 name=firstname> 
    </TD>
    </TR>
    <TR>
    <TD>
    <font face=Arial size=2>Last Name: 
    </TD>
    <TD>
    <INPUT title="Your Google Toolbar can fill this in for you. Select AutoFill" maxLength=100 size=40 name=lastname> 
    </TD>
    </TR>
    <TR>
    <TD>
    <font face=Arial size=2>Email: 
    </TD>
    <TD>
    <INPUT title="Your Google Toolbar can fill this in for you. Select AutoFill" maxLength=100 size=40 name=email>
    </TD>
    </TR>
    <TR>
    <TD>
    <font face=Arial size=2>Phone: 
    </TD>
    <TD>
    <INPUT title="Your Google Toolbar can fill this in for you. Select AutoFill" maxLength=100 size=40 name=phone>
    </TD>
    </TR>
    <TR>
    <TD>
    <font face=Arial size=2>Country: <BR><BR><BR><BR>
    </TD>
    <TD>
    <INPUT title="Your Google Toolbar can fill this in for you. Select AutoFill" maxLength=100 size=40 name=country>
    <p Align=right>
    <input class="BButton" value="Send" name="cmdSubmit" type="submit">
    </align>
    </TD>
    </TR>
    </TABLE>
    </form>
    
    
    </font>
    </body>
    </html>
  • Dameon99
    New Member
    • Jun 2007
    • 57

    #2
    btw this code worked for another site.. but for some reason not this one. Can anyone tell me what Im doing wrong?!

    Comment

    • dmjpro
      Top Contributor
      • Jan 2007
      • 2476

      #3
      Originally posted by Dameon99
      btw this code worked for another site.. but for some reason not this one. Can anyone tell me what Im doing wrong?!
      What is "brochure1" in "document.forms .brochure1.firs tname.value" expression?

      Debasis

      Comment

      • Dameon99
        New Member
        • Jun 2007
        • 57

        #4
        Originally posted by dmjpro
        What is "brochure1" in "document.forms .brochure1.firs tname.value" expression?

        Debasis
        "brochure1" is name of the html file brochure1.html.

        Comment

        • dmjpro
          Top Contributor
          • Jan 2007
          • 2476

          #5
          Originally posted by Dameon99
          "brochure1" is name of the html file brochure1.html.
          all these expression "document.forms .brochure1" should be changed into "document.f orms[0]"

          Debasis

          Comment

          • Dameon99
            New Member
            • Jun 2007
            • 57

            #6
            Originally posted by dmjpro
            all these expression "document.forms .brochure1" should be changed into "document.f orms[0]"

            Debasis

            Why is this? JUst wondering if you could explain it s I understand what I am doing wrong.

            Comment

            • acoder
              Recognized Expert MVP
              • Nov 2006
              • 16032

              #7
              Originally posted by Dameon99
              Why is this? JUst wondering if you could explain it s I understand what I am doing wrong.
              You can't use "brochure1" because it doesn't refer to the form.

              forms[0] is the first form on the page. If you could possibly have more than one form, you would be better off giving your form an id and using document.getEle mentById(form-id).

              Comment

              Working...