Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript
|
Javascript General Javascript discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 13th, 2003, 04:30 PM
Friend of Wrox
 
Join Date: Sep 2003
Posts: 451
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to Ben Horne
Default Yet Another JavaScript Nightmare

Hi all,


I'm having a problem with another piece of Javascript code. This code is actually a validation script that evaluates information that is entered into the form before the form is submitted. I've got the main code already set up to fire when the user hits the submit button. The problem I'm having is with this code:


 }
 // Check for City
 if (this form.City.value=="")
 }
  alert("You must enter in a city");
  thisform.City.focus();
  return false;


This is the rest of the code that I already have


<html>
  <head>
    <title>Adam Smith for Congress 2000 Campaign Site -- Make a Contribution Today </title>
    <script language="Javascript">
<!--
function validation(thisform)
{
 // Check for First Name.
 if (thisform.FirstName.value=="")
 {
  alert("The First Name field is required. The form will not be submitted until you correct the error");
  thisform.FirstName.focus();
  return false;
 }
 // Check for Last Name.
 if (thisform.LastName.value=="")
 {
  alert("The Last Name field is required. The form will not be submitted until you correct the error");
  thisform.LastName.focus();
  return false;
 }
 // Check for City
 if (this form.City.value=="")
 }
  alert("You must enter in a city");
  thisform.City.focus();
  return false;

 return true
}// function validation()
-->
    </script>
  </head>
  <BODY vlink="black" link="black" bgcolor="white">
    <form method="post" onSubmit="return validation(this)" action="http://matcmadison.edu/faculty/jjones/ITIP/post-echo.asp">
      <table border="1" width="400" cellpadding="5" cellspacing="5" align="left">
        <tr>
          <td align="left" bgcolor="#fffff0">

              <p align="left">
                To make an on-line contribution now, please fill out the form below and press
                the submit button. The information and the amount of your contribution will be
                sent directly to the Smith for Congress Campaign.
              </p>

          </td>
        </tr>
        <tr>
          <td align="left" bgcolor="#000099">

              <p align="right">
                First Name*: <input name="FirstName" size="35" maxlength="50"><br>
                Last Name*: <input name="LastName" size="35" maxlength="50"><br>
                Address: <input name="Address" size="35" malenght="40"><br>
                City: <input name="City" size="35"><br>
                State: <input name="State" value="CA" size="2" maxlength="2"> &nbsp;&nbsp;&nbsp;&nbsp;
                Zipcode: <input name="Zip" size="12" maxlength="14"><br>
                Country: <input name="Country" value="US" size="35" maxlength="2"><br>
                Employer: <input name="Employer" size="35" maxlength="50"><br>
                Occupation: <input name="Occupation" size="35" maxlength="50"><br>
                Phone: <input name="Phone" size="35" maxlength="13"><br>
                Email Address: <input name="Email" size="35"><br>
                Credit Cards Accepted<select name="Credit Card" size="1">
                  <option selected value="Visa">Visa</option>
                  <option value="MasterCard">MasterCard</option>
                  <option value="Discover">Discover</option>
                  <option value="American Express">American Express</option>
                </select><br>
                Credit Card #: <input name="CreditCard#" size="15" maxlength="9"><br>
                <br>
                Expires
                <select name="Month" size="1">
                  <option selected value="Month">Month</option>
                  <option value="January">January</option>
                  <option value="February">February</option>
                  <option value="March">March</option>
                  <option value="April">April</option>
                  <option value="May">May</option>
                  <option value="June">June</option>
                  <option value="July">July</option>
                  <option value="August">August</option>
                  <option value="September">September</option>
                  <option value="October">October</option>
                  <option value="November">November</option>
                  <option value="December">December</option>
                </select>
                <select name="Year" size="1">
                  <option selected value="Year">Year</option>
                  <option value="2005">2005</option>
                  <option value="2007">2007</option>
                  <option value="2010">2010</option>
                  <option value="2012">2012</option>
                </select><br>
                Name On Credit Card:<input name="NameOnCard" size="35"><br>
              <p>
              I would like to make a contribution of:
              <P align="left">
                $ <input name="ContributionAmt" size="15">&nbsp; (Minimum $5, Maximum $1000)
              </P>
              <P align="center">
                <input type="submit" name="submit" value="Submit"> <input type="reset" value="Reset">
              <p align="left">*= fields marked with an asterisk are required in order to submit
                the form information successfuly</p>

          </td>
        </tr>
        <tr>
          <td align="left" bgcolor="#fffff0">

              <P align="left">If you prefer to send a contribution by check, please make it
                payable to Adam Smith for Congress and send it to:<br>
                <br>
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Smith for Congress<br>
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 35 S. Raymond Avenue, Suite 206<br>
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Pasadena, CA 91105
              </P>

          </td>
        </tr>
      </table>
    </form>
  </BODY>
</html>


 I did not expect any problems after I finished coding the City validation snippet. The testing revealed bad news. When I hit the submit button after I filled out only one field, I came right to the confirmation page. The validation script didn't even fire like it did before I even coded the city function because the new code (somehow) had overridden it.

I'd appreciate any help,

Thanks,

Ben

Ben
Madison Area Technical College student
-------------------------
I am one of those people that you call "Microsoft Access Freaks". I'm addicted to Access
__________________
Ben Horne
-------------------------
I don\'t want to sound like I haven\'t made any mistakes. I\'m confident I have.

Most likely using FireFox and concocting my next Macromedia Flash project
Snibworks Forums Moderator

Welcome to the New Age
 
Old October 14th, 2003, 01:48 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi Ben,

It's the Typo Ghost hunting you again ;)
Code:
function validation(thisform)
{
... snip ...

 // Check for City
 if (this form.City.value=="")


this form is not a valid object, and it doesn't have a City text box. I am sure you mean thisform......

Cheers,

Imar


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old October 14th, 2003, 11:35 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Also,

// Check for City
 if (this form.City.value=="")
 { //You had }
  alert("You must enter in a city");
  thisform.City.focus();
  return false;
 } //Need to close the if

 return true
}// function validation()


Peter
 
Old October 15th, 2003, 03:56 PM
Friend of Wrox
 
Join Date: Sep 2003
Posts: 451
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to Ben Horne
Default

Imar,

Thanks for the reply. I did find a few typos in the code that I must've missed earlier. I also found that in order for the validation to work properly for some fields, you need to have suppoorting functions so that the rest of the code can fire properly. I guess it was that I didn't have the supporting functions coded yet

Quote:
quote:Originally posted by Imar
 Hi Ben,

It's the Typo Ghost hunting you again ;)
Code:
function validation(thisform)
{
... snip ...

 // Check for City
 if (this form.City.value=="")
this form is not a valid object, and it doesn't have a City text box. I am sure you mean thisform......

Cheers,

Imar


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Ben
Madison Area Technical College student
-------------------------
I am one of those people that you call "Microsoft Access Freaks". I'm addicted to Access





Similar Threads
Thread Thread Starter Forum Replies Last Post
deleting row in a datagrid * nightmare :-( chrismogz ADO.NET 4 October 30th, 2006 05:47 PM
SQL ASP Nightmare... RichChick SQL Server ASP 1 October 5th, 2006 10:00 PM
SQL Query Nightmare gregd SQL Server ASP 1 September 20th, 2006 05:33 PM
Template nightmare xgenerator Dreamweaver (all versions) 2 February 4th, 2005 03:24 PM
Yet another insert nightmare. danielh Dreamweaver (all versions) 1 August 13th, 2003 09:54 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.