2012-02-03 34 views

回答

1

您可以使用keypress()函數。每次用戶輸入一個字符時,都會檢查字符串的長度。如果長度超過13個字符,則可以觸發另一個功能

+0

如何使字符串顯示爲asteriks? – user1170949 2012-02-03 10:41:43

0

我寧願使用keyup()而不是keypress(),因爲它會返回當前輸入長度。按鍵將是 「一步到位之後」

$('input').keyup(function(){ 
    if($this).val().length > 13) 
     $(this).val('*'); 
}); 
+1

這不會將第13個字符更改爲星號或保留實際值。 – Archer 2012-02-03 10:48:00

+0

@Archer哦,錯過了保留值的一部分,但'data()'可以用於某人的提及。就13 *而言,我認爲他會想出自己的東西:) – Johan 2012-02-03 11:39:17

0

看到這個:

HTML:

Input: <input type="text" maxlength="13" id="myInput"/> 

<input type="password" maxlength="13" id="myInputHide" class="hidden"> 

的jQuery:

$(document).ready(
    function() { 
     $(".hidden").hide();      //Hide the asterik field initially 
     $("#myInput").keyup(      //Handle the keyup on the input field 
      function() { 
       if($(this).val().length == $(this).attr("maxlength")) { 
        $(this).hide(); 
        $("#" + this.id + "Hide").show().val($(this).val()); 
       } 
      } 
     ); 

     $("#myInputHide").keyup(      //Handle the keyup on the asterik field 
      function() { 
       if($(this).val().length != $(this).attr("maxlength")) { 
        $(this).hide(); 
        $("#" + this.id.replace(/Hide$/,"")).show().val($(this).val()); 
       } 
      } 
     ); 

    } 

); 

jsfiddle

0

事情是這樣的:

 

var count = 0; 
var maxLength = 4; //or you can gte your maxlength attributes value 
$("#yourInputId").keyup(function() { 
    count++; 
    if(count > maxLength) { 
     var changVal = $(this).val(); 
     var newVal = ""; 
    for(var i = 0; i < maxLength; i++) { 
     newVal += changVal.charAt(i).replace(changVal.charAt(i), "*"); 
    } 
    $(this).val(newVal); 
    } 
}); 
 
是的,你需要微調它

相關問題