javascript - Javascript - 在输入格式中,屏蔽前2个字符

html :<input type="text" class="input" name="secret_word" id="secret_word">


$.fn.mask = function( regexp, matchGroup, callback ) {


 this.on("blur", function(e){


 $(this).data("value", this.value);


 var result;



 while (result = regexp.exec(this.value)) {


 var matches = result.slice(1);



 if (callback){


 var substitute = callback(matches[0]);


 } else {


 var substitute = Array(matches[matchGroup-1].length + 1).join("*");


 }



 matches[matchGroup-1] = substitute;


 this.value = matches.join("");


 }


 })



 this.on("focus", function(e){


 this.value = $(this).data("value") ||"";


 });


 }



 // With Regular expression


 phoneRegexp = new RegExp("(.*?)(.{1})$","g");


 $("#secret_word").mask(phoneRegexp, 2);



参照:https://www.sitepoint.com/community/t/mask-input-fields-without-affecting-validation/37100/15

时间: 原作者:

在表单的事件"onsubmit"上添加一个函数:


<form onsubmit="fix_asterisk();">



 <fieldset>


 <input type="text" class="input" name="secret_word" id="secret_word">


 </fieldset>



 <input type="submit">


</form>



然后,在javascript中添加函数:


<script>


 function fix_asterisk(){


 let input = document.getElementById("secret_word");


 input.value = $(input).data("value");


 }


</script>



原作者:
...