It appears you have not yet registered with DEVPPL. To register please click here... (it's fast, easy and free!)

Forum

Log In Sponsors
Board index Programming JavaScript Forum

Need help on form script

Need help on form script

Postby quezar on Wed Jan 30, 2008 8:21 pm

I don't get why the form submits before it validates the first three forms. Can someone help me by maybe posting a solution plz?

<html>
<body>
<script type="text/javascript">
function check() {
var required = Array('name', 'E-mail', 'description of change');
for (var i=0;i<required.length;i++) {
var box = "document.changes." + required[i];
if (eval(box).value == "") {
alert('Please fill in your ' + eval(box).name);
eval(box).focus();
return false;
}
}
return true;
}
</script>

* denotes a required field
<br>
<br>
<form name="changes" action="#">
<table class="form">
<tr>
<td>* Name:</td>
<td><input name="name" /></td>
</tr>
<tr>
<td>* E-mail:</td>
<td><input name="E-mail" /></td>
</tr>
<tr>
<td>* Description of change:</td>
<td><textarea rows="2" cols="20" name="description of change"></textarea></td>
</tr>
<tr>
<td>Teaching:</td>
<td><input name="teaching" /></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="button" value="Submit" onclick="check();"></td>
</tr>
</table>
</form>
</body>
</html>
quezar
 
Posts: 0
Joined: Wed Jan 30, 2008 8:16 pm

Postby DDragon on Wed Feb 20, 2008 5:25 am

ok here is an example of the way i validate a form:
Code: Select all
<head>
   <script language="javascript">
      function validatetheform()
         {
            if (!document.frmContact.txtGname.value)
         {
            alert("Please enter a Name");
            document.frmContact.txtGname.focus();
            return false;
         }

         if (!document.frmContact.txtEmail.value)
         {
            alert("Please enter your Email");
            document.frmContact.txtEmail.focus();
            return false;
         }

         if (!document.frmContact.rdo2[0].checked &&
               !document.frmContact.rdo2[1].checked &&
               !document.frmContact.rdo2[2].checked &&
               !document.frmContact.rdo2[3].checked )
         {
               alert("How would you like to be contacted?");
            document.frmContact.rdo2[0].focus();

            return false;
         }

            return true;
      }
   </script>
</head>

<body>

<form name="frmContact" method="post" action="cgi-bin/contact.pl" onSubmit="return validatetheform()" onReset="document.frmContact.txtGname.focus()">

      <table width="726" border="0">
      <tr>
         <td colspan="2"><h3>Fields Marked with a * are Required</h3></td>

         <td rowspan="13" width="288">&nbsp;</td>
      </tr>
      <tr>
         <td width="207" align="center"><h3>First Name*</h3>
            <center>
            <input type="text" name="txtGname" size="30">
            </center>
         </td>

         <td width="209"><h3>Last Name</h3>
            <center>
            <input type="text" name="txtSname" size="30">
            </center></td>
         
      </tr>
      <tr>

         <td><h3>Your Email*^</h3>

            <center>
            <input type="text" name="txtEmail" size="30">
            </center></td>
         <td><h3>Address**</h3>
            <center>
            <input type="text" name="txtAddress" size="30">
            </center></td>
      </tr>

      <tr>
         <td><h3>Suburb/Town**</h3>
            <center>
                <input type="text" name="txtSuburb" size="30">
            </center></td>
            <td><h3>Post Code**</h3>
              <center>
                <input type="text" name="txtPstcode" size="30">

            </center></td>
          </tr>

          <tr>
            <td><h3>State**</h3>
              <center>
                <input type="text" name="txtState" size=30>
            </center></td>

            <td><h3>Contry**</h3>
              <center>
                <input type="text" name="txtContry" size=30>
            </center></td>
          </tr>
          <tr>
            <td colspan="2"><h3>How did you hear about us?</h3>              </td>

          </tr>

          <tr>
            <td>
               <p>
               <input type="radio" name="rdo1" value="Yellow Pages">
                 <strong>Yellow pages</strong>
             </p>

             <p>
               <input type="radio" name="rdo1" value="Surfing the internet">
                 <strong>Surfing the internet</strong></p>
         </td>

         <td>
            <p>
               <input type="radio" name="rdo1" value="Word of Mouth">
                <strong>Word of mouth</strong>

             </p>

             <p>
               <input type="radio" name="rdo1" value="Local Paper">
                   <strong>Local Paper</strong>
                  </p>
            </td>


          </tr>

          <tr>
             <td colspan="2" align="center"><h3>Would You like to recieve news about updates?</h3></td>
          </tr>

          <tr>
            <td colspan="2" align="center"><p>
              <input type="radio" name="rdo2" value="E-Mail Newsletter">
              <strong>E-Mail Newsletter^</strong> </p>

              <p>
                <input type="radio" name="rdo2" value="Newsletter">
                <strong>Newsletter^</strong></p>
              <p>
              <input type="radio" name="rdo2" value="Dont Send me information">
              <strong>Don't Send me information</strong></p>              </td>
          </tr>

          <tr>
            <td colspan="2" align="center"><h3>Comments</h3></td>
          </tr>

          <tr>
            <td colspan="2" rowspan="2" align="center"><textarea name="txtRequest" cols=60 rows=8></textarea></td>
          </tr>
          <tr>

          </tr>
          <tr>
            <td><div align="center">
              <input name="Submit" type="Submit" value="Submit Information">
            </div></td>

          <td><div align="center">
              <input name="Reset" type="Reset" value="Reset Form">
            </div></td>

          </tr>
        </table>
      <center>^ see our <a href="privacy.html">privacy statment</a> for concerns over these fields<br />
      ** Only required if you wish to recieve updates via snail mail</center>
      </form>



now mind you its an old form i used in this example as now i dont use tables to line it all up but thats roughly how i set up a form validation.

Hope this helps,

DDragon
DDragon
50+ Club
 
Posts: 93
Joined: Wed Jun 20, 2007 12:51 am
Location: Australia

Postby leonard on Wed Feb 20, 2008 3:57 pm

try this:

replace:
Code: Select all
<input type="submit" name="button" value="Submit" onclick="check();">

with
Code: Select all
<input type="button" name="button" value="Submit" onclick="check();this.form.submit();">


I have not tested the code.

cheers!
- leonard
:%s/^M//
There are 10 kinds of people:
Those who understand binary and those who don't.
User avatar
leonard
100+ Club
 
Posts: 147
Joined: Tue Dec 18, 2007 7:11 am
Location: Switzerland


Who is online

Users browsing this forum: No registered users and 6 guests