2014-09-19 59 views
0

我有一個表單,我需要禁用除地址字段以外的所有字段上的空格鍵。如何禁用地址字段以外的所有字段的空格鍵?

有人能告訴我如何做到這一點?

我想使所有字段不能使用空格鍵,除了地址字段。我有其他驗證,但我需要禁用空格鍵。

謝謝!

<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> 
+0

*有人可以告訴我如何做到這一點嗎?* - SO不是那個意思,你需要嘗試一些東西。 – LcSalazar 2014-09-19 15:17:13

回答

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與「地址」的值。

接下來,我查找了哪個鍵碼是空格鍵(32)。

最後,我們添加一個按鍵事件處理程序到匹配的元素,並告訴處理程序阻止默認操作並返回false停止執行。

相關問題