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
    Jan 2003
    Location
    Belgium
    Posts
    74
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Form Validation Help

    Hiya,

    It may be a stupid question, but I am kind of new in JavaScript.

    I have a form, and in that form, I want to calculate the price of a movie.
    First you have to choose between VHS or DVD and then between the number you want to buy of the movie.

    How do I make a script that does that, but it may only be activated when a movie is chosen.

    Here is a part of the code:
    Code:
    <form>
    <input type="text" name="moviename" value="" size="30" maxlength="100" disabled>
    <a href="#" onclick="window.open('pop1.html','','top=100,left=100,width=350,height=400'); return false;">Choose your movie</a>
    <select name="type">
                                  <option value="vhs">VHS</option>
                                  <option value="dvd">DVD</option>
                                </select>
    <select name="how_many">
                                  <option value="1">1</option>
                                  <option value="2">2</option			              <option value="3">3</option>
    	              <option value="4">4</option>
    	              <option value="5">5</option>
                                </select>
    <input type="text" name="price" value="" size="10">
    </form>
    The price of the movie can be put in the popup.

    I hope you understand what I'm meaning, and that you can help me

    See ya
    SToFFeL

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    41° 8' 52" N -95° 53' 31" W
    Posts
    3,660
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Something like this?

    Code:
    
    <html>
    <head>
       <title>Get the price</title>
    <script type="text/javascript">
    <!--
    function formatCurrency(num) {
       num = num.toString().replace(/\,/g,"");
       num = !isNaN(num) ? Math.round(num * 100) / 100 : 0;
       num.toString().indexOf(".") == -1 ? num += ".00" : void 0;
       while(/\.\d{0,1}$/.test(num)) num += "0";
       var objRegExp  = new RegExp('(-?\[0-9]+)([0-9]{3})');
       while(objRegExp.test(num)) num = num.toString().replace(objRegExp,'$1,$2');
       return num;
    }
    function calculatePrice(movieType,movieQuantity) {
       var moviePrice = new Array(0,9.95,14.95)
       if(movieType > 0 && movieQuantity != "") {
          document.forms[0].price.value = "$" + formatCurrency(moviePrice[movieType] * movieQuantity);
          // window.open('pop1.html','','top=100,left=100,width=350,height=400')
       }
       else {
          document.forms[0].price.value = "";
       }
    }
    // -->
    </script>
    </head>
    <body>
       <form id="form1" action="javascript://" onsubmit="return false">
          <table>
             <tr>
                <td>Movie:</td>
                <td><input type="text" name="moviename" value="Enemy Mine" size="30" onfocus="this.blur()" /></td>
             </tr>
             <tr>
                <td>Format:</td>
                <td>
                   <select name="movietype" onchange="calculatePrice(this.form.movietype.selectedIndex,this.form.how_many.options[this.form.how_many.selectedIndex].value)">
                      <option value="">------</option>
                      <option value="vhs">VHS</option>
                      <option value="dvd">DVD</option>
                   </select>
                </td>
             </tr>
             <tr>
                <td>Quantity:</td>
                <td>
                   <select name="how_many" onchange="calculatePrice(this.form.movietype.selectedIndex,this.form.how_many.options[this.form.how_many.selectedIndex].value)">
                      <option value="">--</option>
                      <option value="1">1</option>
                      <option value="2">2</option>
                      <option value="3">3</option>
                      <option value="4">4</option>
                      <option value="5">5</option>
                   </select>
                </td>
             </tr>
             <tr>
                <td colspan="2">&nbsp;</td>
             </tr>
             <tr>
                <td>Price:</td>
                <td>
                   <input type="text" name="price" value="" size="10" onfocus="this.blur()" />
                </td>
             </tr>
          </table>
       </form>
    </body>
    </html>
    
    Hope this helps.
    Last edited by whammy; 01-19-2003 at 06:16 PM.
    Former ASP Forum Moderator - I'm back!

    If you can teach yourself how to learn, you can learn anything. ;)

  • #3
    New Coder
    Join Date
    Jan 2003
    Location
    Belgium
    Posts
    74
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thnx a lot

    I'll take a look at it
    SToFFeL


  •  

    Posting Permissions

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