2015-09-04 45 views
-1

我正在創建一個短信發件人。我有一個文本框。現在我正在使用jQuery Keyup事件計數文本長度。jQuery Keyup Count TextLength for SMS

<form> 
    <div class="form-group"> 
     <label for="sms">SMS</label> 
     <textarea rows="5" class="form-control" id="sms" name="sms"></textarea> 
     <span class="pull-right" id="smslength">0</span> 
    </div> 
    <button type="submit" class="btn btn-default">SMS Gönder</button> 
</form> 
<script type="text/javascript"> 
    $('#sms').keyup(function(){ 
     sms = $(this).val(); 
     $('#smslength').html(sms.length); 
    }) 
</script> 

但對於特殊字符的短信計數2(如S,S,G,G,C,I,I)

我希望當我輸入上面的字符數爲兩個字符。有多個字節的jQuery的函數嗎?

enter image description here

回答

0

只是包括如果塊檢查按鍵的字符代碼(如果它是提到的那些之一)

<script type="text/javascript"> 
var codes = ["Ş", "ş", "Ğ", "ğ", "ç", "ı", "İ"]; 
    $('#sms').keyup(function(e){ 
    if($.inArray(e.charCode, codes)){ 
     //double value 
    }else{  
     //normal value 
    } 
    }) 
</script> 
+0

in here($ .inArray(e.charCode,codes)codes are correct or it should car? –

+0

應該是汽車或你給你的代碼陣列(對不起的錯字) –

0

這是我的HTML代碼

<form> 
    <div class="form-group"> 
     <label for="sms">SMS</label> 
     <textarea rows="5" class="form-control" id="sms" name="sms" maxlength="745"></textarea> 
     <span class="pull-right"><span id="smslength">0</span>/<span id="maxsmslength">745</span> [SMS Adeti: <span id="smscount">0</span>]</span> 
    </div> 
    <button type="submit" class="btn btn-default">SMS Gönder</button> 
</form> 

這是我的jQuery代碼

<script type="text/javascript"> 
    $('#sms').keyup(function(){ 
     doubleChars = ["Ş", "ş", "Ğ", "ğ", "ç", "ı", "İ"]; 

     sms = $(this).val(); 
     textlength = sms.length; 

     smslength = 0; 

    if(textlength > 0){ 
      for(i=0; i < textlength; i++){ 
       if($.inArray(sms.charAt(i), doubleChars) !== -1){ 
        //double value 
        smslength += 2; 
       }else{  
        //normal value 
        smslength++ 
       } 
      } 
     } 

     $('#smslength').html(smslength); 

     if(smslength == 0){ 
      smscount = 0; 
     } else if(smslength > 0 && smslength <= 155){ 
      smscount = 1; 
     } else if(smslength > 155 && smslength <= 298){ 
      smscount = 2; 
     } else if(smslength > 298 && smslength <= 447){ 
      smscount = 3; 
     } else if(smslength > 447 && smslength <= 596){ 
      smscount = 4; 
     } else if(smslength > 596 && smslength <= 745){ 
      smscount = 5; 
     } 

     $('#smscount').html(smscount); 
    })