JavaScript表单不起作用

时间:2013-02-26 19:28:51

标签: javascript forms

我试图创建一个javascript表单,但我无法让它工作。出现的错误是

  • 邮政编码应使用字母和数字:
  • 地址应为字母数字:
  • 请将每本杂志限制在500份以下

    function display(){      var totprice;      var fname,lname,fullname,addr,postocde,email,telephone,lstitem,quantity,gender;      var prditem1,prditem2,prditem3,summer2012,autumn2012,winter2012,totqty;      var orddate;      fname = document.form1.fname.value;      lname = document.form1.lname.value;      // fullname = fname +“”+ lname;      gender = document.form1.gender.value;      addr = document.form1.address.value;      postcode = document.form1.address.value;      email = document.form1.email.value;      telephone = document.form1.telephone.value;      prditem1 = document.form1.summer.value;      prditem2 = document.form1.autumn.value;      prditem3 = document.form1.winter.value;      summer2012 = parseInt(document.form1.summer2012.value);      autumn2012 = parseInt(document.form1.autumn2012.value);      winter2012 = parseInt(document.form1.winter2012.value);      totqty = summer2012 + autumn2012 + winter2012;      orddate = new Date();      dispdate = orddate.getMonth()+ 1 +“ - ”+ orddate.getDate()+“ - ”+ orddate.getYear();      var alertmsg ='';      var alphabetic = / ^ [a-zàâçéèêëîïôûùüÿñ - ] * $ / i      var alphanumeric = /^[a-zA-Z0-9/./,/-/\n]+$/;      var addrtxt = addr.replace(/(\ x0a \ x0d | \ x0d \ x0a)/ g,“\ n”);      var chkpostcode = / ^((GIR 0AA)|(([[A-PR-UWYZ] [A-HK-Y]?[0-9] [0-9]?)|(([A-PR-UWYZ ] [0-9] [A-HJKSTUW])|([A-PR-UWYZ] [A-HK-Y] [0-9] [ABEHMNPRV-Y])))[0-9] [ABD-HJLNP -uw-Z] {2}))$ /      var chktelephone = / ^ 0 \ d {9,10} $ /      var chkEmail = /^.+@.+..+$/      var chkquantity = / ^([1-9]?\ d | [1-4] \ d {2} | 500)$ /      if(chkEmail.test(email)== false){          alertmsg = alertmsg +“请输入有效的电子邮件。” +“\ n”;      }      if((alphabetic.test(fname)== false)||(alphabetic.test(lname)== false)){          alertmsg = alertmsg +“名称应为字母:”+“\ n”;      }      if(chktelephone.test(telephone)== false){          alertmsg = alertmsg +“电话应该是数字:”+“\ n”;      }      if(chkpostcode.test(postcode)== false){          alertmsg = alertmsg +“邮政编码应该是字母和数字:”+“\ n”;      }      if(alphanumeric.test(addrtxt)== false){          alertmsg = alertmsg +“地址应为字母数字:”+“\ n”;      }      var gender = document.form1.gender [0] .checked;      var gender1 = document.form1.gender [1] .checked;      if(!gender&&!gender1){          alertmsg = alertmsg +“请选择你的性别\ n”      }      if(((document.form1.summer.checked)&&(summer2012< = 0))||((document.form1.autumn.checked)&&(autumn2012< = 0))|| ((document.form1.winter.checked)&&(winter2012< = 0))){          alertmsg = alertmsg +“请输入数量”+“\ n”;      } else if(((!document.form1.summer.checked)&&(summer2012> 0))||((!document.form1.autumn.checked)&&(autumn2012> 0)) ||((!document.form1.winter.checked)&&(winter2012> 0))){          alertmsg = alertmsg +“请选择产品”+“\ n”;      }      var f = document.form1;      if(!f.summer.checked&&!f.autumn.checked&&!f.winter.checked){          alertmsg = alertmsg +“请至少选择一个版本的杂志”+“\ n”;      }      if(chkquantity.test(quantity)< = 500){          alertmsg = alertmsg +“请将每本杂志限制在500份以下:”+“\ n”;      }      if(alertmsg!=“”){          alertmsg =“请输入以下值\ n”+ alertmsg;          警报(ALERTMSG);      } else {          fullname = formatName(fname,lname);          totprice = totalprice();      }      // alert(“感谢您提交详细信息”);      function totalprice(){          var totprice = 0;          var price = new Array();          价格[0] = 20.00;          价格[1] = 20.00;          价格[2] = 20.00;          var quantity = new Array();          quantity [0] = parseInt(document.form1.summer2012.value);          quantity [1] = parseInt(document.form1.autumn2012.value);          quantity [2] = parseInt(document.form1.winter2012.value);          for(i = 0; i< 3; i ++){              totprice = totprice + quantity [i] * price [i];          }          return(totprice);      }  }

有谁能建议我做错了什么?真的很感激任何帮助。感谢

这是html -

<form name="form1" method="post">
    <fieldset id="fieldset">
       <legend id="legend">Order the latest Saraysounds Magazine</legend>

<table width="500" border=0 align="left" cellpadding="4" cellspacing="4">


        <tr>
          <td colspan="2"><div align="left">First
              Name</div></td>
          <td colspan="2"><input type="text" name="fname"/>
          </td>
        </tr>
        <tr>
          <td colspan="2"><div align="left">Second
              Name</div></td>
          <td colspan="2">
            <input type="text" name="lname"/>
         </td>
        </tr>

        <tr>
          <td colspan="2"><div align="left">Gender</div></td>
          <td colspan="2">
            <input type="radio" name="gender" value="M"/>
           Male 
            <input type="radio" name="gender" value="F"/>
            Female </td>
        </tr>

        <tr>
          <td colspan="2"><div align="left">Address</div></td>
          <td colspan="2"><textarea name="address" cols="30" rows=5 col=40 wrap=soft>     </textarea>
          </td>
          </tr>
          <tr>
           <td colspan="2"><div align="left">Postcode</div></td>
          <td colspan="2">  <input type="text" name="postcode"/>
      </td>
    </tr>

       <tr>
      <td colspan="2"><div align="left">Email</div></td>
      <td colspan="2">
        <input type="text" name="email"/>
      </td>
    </tr>

     <tr>
      <td colspan="2"><div align="left">Telephone</div></td>
      <td colspan="2">
        <input type="text" name="telephone"/>
      </td>
    </tr>


    <tr>
             <td colspan="4"><center>
               <strong>Select
               Magazine</strong>
             </center></td>
             </tr>
             <tr>
             <td width="123"><center>
               <strong>Product Name</strong>
             </center></td>
             <td width="30" ><center>
               <strong></strong>
             </center></td>
               <td ><center>
               <strong>Price</strong>
             </center></td>
               <td>
              <strong>Quantity</strong>
             </td>
             </tr>
           <tr>
             <td  align="right">Summer 2012
               </td>
             <td>
               <input type="checkbox" name="summer" value="Summer 2012"/>
           </td>
             <td align="center" width="69"><div align="right">20.00 </div></td>
             <td width="216"><input name="summer2012" type="text" size="5" value="0"/></td>
           </tr>
         <tr>
             <td align="right">Autumn 2012
             </td>
             <td>
               <input type="checkbox" name="autumn" value="Autumn 2012"/>
           </td>
             <td align="center" width="69"><div align="right">20.00 </div></td>
             <td><input name="autumn2012" type="text" size="5" value="0"/></td>
         </tr>
         <tr>
             <td align="right">Winter 2012
           </td>
             <td>
               <input type="checkbox" name="winter" value="Winter 2012"/>
          </td>
             <td align="center"  width="69"><div align="right">20.00</div></td>
             <td><input name="winter2012" type="text" size="5" value="0"/></td>
         </tr>

           <tr>
             <td align="center" colspan="4"><input name="button" type="button" onClick="javascript:display()" value="Submit"/>
              <input type="reset" value="Clear Form"/>
             </td>
           </tr>
      </table>

 </fieldset>
</form>

1 个答案:

答案 0 :(得分:0)

修改这些:

脚本

var alphanumeric = /^[a-zA-Z0-9\.\,\-\n]+$/;

var quantity = new Array();
 quantity[0] = parseInt(document.form1.summer2012.value);
 quantity[1] = parseInt(document.form1.autumn2012.value);
 quantity[2] = parseInt(document.form1.winter2012.value);
 if (quantity[0] > 500 || quantity[1] > 500 || quantity[2] > 500) {
     alertmsg = alertmsg + "Please limit each magazine to 500 copies or less:" + "\n";
 }

HTML

<td colspan="2"><textarea name="address" cols="30" rows=5 col=40 wrap=soft></textarea>