Javascript utility small function using the introduction

  • 2020-03-26 23:59:52
  • OfStack

1. Block non-numeric input, except backspace
 
<script type="text/javascript"> 
var checkNo = function(e){ 
var keyCode = e.charCode ? e.which : e.keyCode; 
if(keyCode == 8){ 
return true; 
} 
var realkey = String.fromCharCode(keyCode); 
var reg = "\d"; 
var regExp = new RegExp(reg); 
return regExp.test(realkey); 
} 
</script> 

2. Limit the maximum length of the input string
 
//Limit the maximum input length
var checkMaxL = function(length) { 
var regC = /[^ -~]+/g; 
var regE = /D+/g; 
var str = jQuery("#remark").val(); 

if (regC.test(str)){ 
jQuery("#remark").val(str.substring(0, length)); 
} 

if(regE.test(str)){ 
jQuery("#remark").val(str.substring(0, length)); 
} 
} 

Related articles: