2012-10-29 105 views
0

你好我短信面板上的工作,我要檢查,文本區域的長度現在我使用這些代碼:當我將文本粘貼到文本區域警報顯示驗證長度

 $("#txt").keydown(function() { 
      var len = $(this).val().length; 
      $("#lbl").text(len); 
      if (len <= 70) { 
       $("#lbl").text("One SMS"); 
       if (confirm("more than one sms are you sure?")) { 
        $("#lbl").text(len); 
       } 
      } 
      else if (len >= 71 && len <= 133) { 
       $("#lbl").text("two SMS"); 
       if (confirm("more than two sms are you sure?")) { 
        $("#lbl").text(len); 
       } 
      } 
      else if (len > 134 && len <= 199) { 
       $("#lbl").text("three SMS"); 
       alert("more than three sms are you sure?"); 
      } 
     }); 

我想和選擇沒有輸入任何東西

謝謝你的建議?

+0

第一個建議 - 提出可回答的問題。 「不完美」不是一個解釋。 – zerkms

+0

看看這個 [textarea長度計數](http://stackoverflow.com/questions/4506959/jquery-text-area-length-count) –

+0

@zerkms例如,當我複製文本到textarea它不工作。 –

回答

2

你的意思是:

else if (len >= 71 && len <= 133) { 
    $("#lbl").text("two SMS"); 
    if (confirm("more than two sms are you sure?")) { 
     $("#lbl").text(len); 
    } 
} 
else if (len >= 134 && len <= 199) { 
     $("#lbl").text("three SMS"); 
     alert("more than three sms are you sure?"); 
} 
+0

這不是問題。當你達到一定數量的字符時 - 警報出現。 – zerkms

+0

不,我必須檢查一個短信的長度,例如第一個短信的長度是70,第二個是63,另一個是66 –

0

,但它不是完美的工作,

一個錯誤我看得出來,是不是使它完全是工作,你已經使用該keydown事件來尋找長度,所以如果你複製和粘貼沒有Keydown事件發生,所以你不能在那裏找到長度,所以你可以在文本框的onchange事件中寫入相同的函數,這樣當你複製和粘貼你的數據也可以找到長度

$("input").change(function(){ 
    //your function of finding the length goes here 
    }); 

如果您發佈確切的問題,將更容易獲得幫助。

希望這會有所幫助:D