在复选框验证javascript中检查数量

时间:2013-02-27 16:32:53

标签: javascript checkbox

最近我发布了一个关于为什么我的javascript表单不起作用的查询。三个问题之一是 - 复选框和数量

我需要使用我的chkquantity正则表达式测试每个输入值,并确保已经订购了超过零,因为我的代码仅检查已选中复选框但未提供数量或提供数量的情况但是它是复选框未经检查,因此这些测试不排除未提交复选框且所有数量均设置为零的提交。

然后我需要为totqty小于或等于零

生成进一步的测试

正则表达式 -         var chkquantity = ^([1-9]?\ d | [1-4] \ d {2} | 500)$ /

测试

if (((document.form1.summer.checked) && (summer2012 <=0)) 
|| ((document.form1.autumn.checked) && (autumn2012 <=0)) 
|| ((document.form1.winter.checked) && (winter2012 <=0)))
{ 
alertmsg = alertmsg + "Please enter Quantity" +"\n"; 
}
else if (((!document.form1.summer.checked) && (summer2012 >0)) 
|| ((!document.form1.autumn.checked) && (autumn2012 >0)) 
|| ((!document.form1.winter.checked) && (winter2012 >0))) { 
alertmsg = alertmsg + "Please choose Product" +"\n";
}

if (alertmsg != "")
{
alertmsg = "Please enter the following values \n" + alertmsg;
alert(alertmsg);
} 

var f = document.form1;
if (!f.summer.checked && !f.autumn.checked && !f.winter.checked) {
alertmsg = alertmsg + "Please choose at least one edition of the magazine" +"\n";

}

if ((chkquantity.test(quantity) <= 500) {
alertmsg = alertmsg + "Please limit each magazine to 500 copies or less:" + "\n";
}

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">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>

0 个答案:

没有答案