如何禁用除地址字段以外的所有字段的空格键?

时间:2014-09-19 15:09:31

标签: javascript

我有一个表单,我需要在除地址字段之外的所有字段上禁用空格键。

有人可以告诉我该怎么做吗?

我想让所有字段都无法使用空格键,地址字段除外。我有其他验证,但我需要禁用空格键。

谢谢!

<form method="POST" action="" name="MyForm" id="MyForm">
            <div class="input-field">
              <select name="insuranceType" class="form-control input-lg" id="exampleInputLarge-select" onChange="window.document.location.href=this.options[this.selectedIndex].value;" value="GO" style="color:#0d5973 !important;">
               <option disabled alue="Please Choose an Insurance" selected>Please Choose an Insurance Type</option
               ><option value="http://something.com/qualify/not-qualified.html">Medicare</option>
               <option value="http://something.com/qualify/not-qualified.html">Medicaid</option>
               <option value="#">Private Insurance</option>
              </select>
             </div>
                <p>
                    <input name="firstName" type="text" class="form-control form-control-name" id="firstName" placeholder="First Name" size="33" maxlength="50">
                </p>
                <p>
                    <input name="LastName" type="text" class="form-control form-control-name" id="LastName" placeholder="Last Name" size="33" maxlength="50">
                </p>
                <p>
                    <input name="email" type="text" class="form-control form-control-email" id="email" placeholder="Email Address" size="33" maxlength="50">
                </p>
                <p>
                    <input name="phone" type="text" class="form-control form-control-phone" id="phone" placeholder="Phone Number" size="10">
                </p>
                <p>
                    <input type="hidden" name="SUB_1" value="">
                </p> 
                <p>
                    <input type="hidden" name="AFFILIATE_ID" value="">
                </p>
                <p>
                    <input name="birthdate" type="text" class="form-control form-control-name" id="birthdate" placeholder="Birthdate" size="33">
                </p>
                <p>
                    <input name="address" type="text" class="form-control form-control-email" id="address" placeholder="Address" size="33">
                </p>
                <p>
                    <input name="city" type="text" class="form-control form-control-phone" id="city" placeholder="City" style="margin-top: 20px!important;" size="33">
                </p>
                <div class="form-group">

                <p><div class="input-field" name="state">
             <select value="State" name="state" class="form-control input-lg" id="exampleInputLarge-select" style="height: 40px!important; margin-top: 20px!important; color:#0d5973 !important;">
             <option value="">Please Choose a State:</option>
               <option value="Alabama">Alabama</option>
               <option value="Arizona">Arizona</option>      
               <option value="California">California</option>         
               <option value="Colorado">Colorado</option>       
               <option value="Connecticut">Connecticut</option>          
               <option value="Delaware">Delaware</option>       
               <option value="District Of Columbia">District Of Columbia</option>                   
               <option value="Florida">Florida</option>      
               <option value="Georgia">Georgia</option>      
               <option value="Illinois">Illinois</option>       
               <option value="Indiana">Indiana</option>      
               <option value="Iowa">Iowa</option>
               <option value="Kentucky">Kentucky</option>       
               <option value="Maine">Maine</option>    
               <option value="Maryland">Maryland</option>       
               <option value="Massachusetts">Massachusetts</option>            
               <option value="Michigan">Michigan</option>       
               <option value="Minnesota">Minnesota</option>        
               <option value="Missouri">Missouri</option>       
               <option value="Montana">Montana</option>      
               <option value="Nebraska">Nebraska</option>       
               <option value="New Hampshire">New Hampshire</option>            
               <option value="New Jersey">New Jersey</option>         
               <option value="New Mexico">New Mexico</option>         
               <option value="New York">New York</option>       
               <option value="North Carolina">North Carolina</option>             
               <option value="North Dakota">North Dakota</option>           
               <option value="Ohio">Ohio</option>   
               <option value="Oklahoma">Oklahoma</option>       
               <option value="Pennsylvania">Pennsylvania</option>           
               <option value="Rhode Island">Rhode Island</option>           
               <option value="South Carolina">South Carolina</option>             
               <option value="Tennessee">Tennessee</option>        
               <option value="Texas">Texas</option>    
               <option value="Utah">Utah</option>   
               <option value="Virginia">Virginia</option>       
               <option value="Washington">Washington</option>         
               <option value="West Virginia">West Virginia</option>            
               <option value="Wisconsin">Wisconsin</option>                                          
               </select>       
       </div>
                </p>

                <p>
                    <input name="zip" type="text" class="form-control form-control-email" id="zip" placeholder="Zip Code" size="33" maxlength="5">
                </p>
                <p>
                    <input name="insuranceComp" type="text" class="form-control form-control-phone" id="insuranceComp" placeholder="Insurance Company" size="33" maxlength="30">
                </p>
                <p>
                    <input name="memberID" type="text" class="form-control form-control-phone" id="memberID" placeholder="Member ID or Policy Number" size="33">
                </p>
                <p>
                    <input name="RxBin" type="text" class="form-control form-control-phone" id="RxBin" placeholder="RX BIN Number" size="33">
                </p>
                <p>
                    <input name="PcnNumber" type="text" class="form-control form-control-phone" id="PcnNumber" placeholder="PCN Number" size="33">
                </p>
                <p>
                    <input name="groupID" type="text" class="form-control form-control-phone" id="groupID" placeholder="RX Group Number" size="33">
                </p>
                <p>
                    <input name="insurancePhone" type="text" class="form-control form-control-phone" id="insurancePhone" placeholder="Insurance Phone Number" size="33" maxlength="10">
                </p>
                    <input type="submit" class="btn pi-btn col-xs-12 col-sm-offset-2 col-sm-8 col-md-offset-2 col-md-8 col-lg-offset-2 col-lg-8" id="" placeholder="Submit" style="background-color:#009ca8!important;color: #FFF!important;" onClick="MM_validateForm('firstName','','R','LastName','','R','email','','RisEmail','phone','','RisNum','birthdate','','R','address','','R','city','','R','zip','','RisNum','insuranceComp','','R','memberID','','R','RxBin','','R','PcnNumber','','R','groupID','','R','insurancePhone','','RisNum');return document.MM_returnValue" value="Submit">
                </p>
              </form>

1 个答案:

答案 0 :(得分:0)

var spaceBarKeyCode = 32;
$('input[type="text"]:not([name="address"])').keypress(function (e) { 
    if (e.keyCode ===  spaceBarKeyCode) { 
        e.preventDefault(); 
        return false;
    } 
});

我还没有这样做,但它正朝着正确的方向前进。这就是我们正在做的事情:

$('input[type="text"]:not([name="address"])')

选择type="text"属性中没有name属性的所有元素,其值为&#34;地址&#34;。

接下来,我查找了哪个键代码是空格键(32)。

最后,我们向匹配元素添加一个按键事件处理程序,并告诉处理程序阻止默认操作并返回false以停止执行。