1、设置 input 只能输入正整数(下面有两个方法都能实现):
/**
* 设置 input 只能输入正整数
* @param a
*/
function inputInt(a){
$(a).off('keyup').on('keyup',function(){
if(this.value.length==1){
this.value=this.value.replace(/[^1-9]/g,'');
}else{
this.value=this.value.replace(/\D/g,'');
}
});
$(a).off('paste').on('paste',function(){
var textArea = $(this);
setTimeout(function(){
if(textArea.val().length==1){
textArea.val(textArea.val().replace(/[^1-9]/g,''));
}else{
textArea.val(textArea.val().replace(/\D/g,''));
}
}, 200);
});
$(a).off('change').on('change',function(){
if(this.value!='' && this.value!=null){
if(this.value.indexOf(".")< 0){
this.value = this.value;
}else{
this.value = this.value.split('.')[0];
}
}else{
this.value = '0';
}
});
}
/**
* 限制输入框正整数2
* @param a input选择器
* @returns
*/
function inputInt2(a){
$(a).off('keyup').on('keyup',function(){
this.value=this.value.replace(/[^\d]/g,'');
});
$(a).off('paste').on('paste',function(){
this.value=this.value.replace(/[^\d]/g,'');
});
$(a).off('change').on('change',function(){
this.value=this.value.replace(/[^\d]/g,'');
});
}
2、设置 input 只能输入正数,并且显示小数点后两位
/**
* 设置 input 只能输入正数,并且显示小数点后两位
* @param a
*/
function inputZFloat2(a){
$(a).off('keyup').on('keyup',function(){
this.value=this.value.replace(/[^\d.]/g,''); //清除“数字”和“.”以外的字符
this.value=this.value.replace(/\.{2,}/g,"."); //只保留第一个. 清除多余的
this.value = this.value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");
this.value = this.value.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3');//只能输入两个小数
if(this.value.indexOf(".")< 0 && this.value!='' && this.value!=null){
this.value = parseFloat(this.value);
}
});
$(a).off('paste').on('paste',function(){
var textArea = $(this);
setTimeout(function(){
this.value=this.value.replace(/[^\d.]/g,''); //清除“数字”和“.”以外的字符
this.value=this.value.replace(/\.{2,}/g,"."); //只保留第一个. 清除多余的
this.value = this.value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");
this.value = this.value.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3');//只能输入两个小数
if(this.value.indexOf(".")< 0 && this.value!='' && this.value!=null){
this.value = parseFloat(this.value);
}
}, 200);
});
$(a).off('change').on('change',function(){
if(this.value!='' && this.value!=null){
if(this.value.indexOf(".")< 0){
this.value = this.value+'.00';
}else{
if(this.value.split('.')[1].length==0){
this.value = this.value+'00';
}else if(this.value.split('.')[1].length==1){
this.value = this.value+'0';
}else{
this.value = this.value;
}
}
}else{
this.value = '0.00';
}
});
}
3、限制输入框小数位(下面两个方法均能实现):
/**
* 限制输入框小数位
* @param a 输入框选择器
* @param digit 小数位长度
*/
function inputFloatDigit(a,digit){
eval("var reg = /^(\-)*(\d+)\.(\d{"+digit+"}).*$/");
console.log(digit);
$(a).off('keyup').on('keyup',function(){
this.value=this.value.replace(/[^\d.]/g,''); //清除“数字”和“.”以外的字符
this.value=this.value.replace(/\.{2,}/g,"."); //只保留第一个. 清除多余的
this.value = this.value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");
this.value = this.value.replace(reg,'$1$2.$3');//只能输入两个小数
if(this.value.indexOf(".")< 0 && this.value!='' && this.value!=null){
this.value = parseFloat(this.value);
}
});
$(a).off('paste').on('paste',function(){
this.value=this.value.replace(/[^\d.]/g,''); //清除“数字”和“.”以外的字符
this.value=this.value.replace(/\.{2,}/g,"."); //只保留第一个. 清除多余的
this.value = this.value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");
this.value = this.value.replace(reg,'$1$2.$3');//只能输入digit个小数
if(this.value.indexOf(".")< 0 && this.value!='' && this.value!=null){
this.value = parseFloat(this.value);
}
});
$(a).off('change').on('change',function(){
if(this.value!='' && this.value!=null){
if(this.value.indexOf(".")< 0){
this.value = this.value+'.';
for(var i=0; i<digit; i++){
this.value += '0';
}
}else{
var len = this.value.split('.')[1].length;
if(len<digit){
this.value = this.value
for(var i=0; i<(digit-len); i++){
this.value += '0';
}
}else{
this.value = this.value.split('.')[0]+'.'+this.value.split('.')[1].substr(0,digit);
}
}
}else{
this.value = '0.';
for(var i=0; i<digit; i++){
this.value += '0';
}
}
});
}
/**
* 限制输入框小数位
* @param a input选择器
* @param decimal 限制小数位
* @returns
*/
function inputDecimal(a,decimal){
if(decimal == undefined){
decimal='0,'
}
var reg = new RegExp("(\\d+)(\\.?)(\\d{"+decimal+"}).*$");
$(a).off('keyup').on('keyup',function(){
this.value=this.value.replace(/[^\d.]/g,''); //清除“数字”和“.”以外的字符
this.value=this.value.replace('.','$#$').replace(/\./g,'').replace("$#$","."); //只保留第一个. 清除多余的
this.value=this.value.replace(reg,'$1$2$3');
if(this.value.indexOf(".")< 0 && this.value!='' && this.value!=null){
this.value = parseFloat(this.value);
}
});
$(a).off('paste').on('paste',function(){
this.value=this.value.replace(/[^\d.]/g,'');
this.value=this.value.replace('.','$#$').replace(/\./g,'').replace("$#$",".");
this.value=this.value.replace(reg,'$1$2$3');
if(this.value.indexOf(".")< 0 && this.value!='' && this.value!=null){
this.value = parseFloat(this.value);
}
});
$(a).off('change').on('change',function(){
this.value=this.value.replace(/[^\d.]/g,'');
this.value=this.value.replace('.','$#$').replace(/\./g,'').replace("$#$",".");
this.value=this.value.replace(reg,'$1$2$3');
if(this.value.indexOf(".")< 0 && this.value!='' && this.value!=null){
this.value = parseFloat(this.value);
}
});
}
4、限制输入框输入长度
/**
* 限制输入框输入长度
* @param a 输入框选择器
* @param len 限制长度
* @param type 用于判断该输入框是否只能输入数字还是任意字符
*/
function inputLen(a,len,type){
if(type!=undefined){
eval("var reg = /^(\d{1,"+len+"})/");
}else{
eval("var reg = /^(.{1,"+len+"})/");
}
var strLen = 0;
$(a).off('keyup').on('keyup',function(){
this.value=this.value.replace(reg,'$1');
if(this.value!='' && this.value!=null){
this.value = this.value;
}
});
$(a).off('paste').on('paste',function(){
this.value=this.value.replace(reg,'$1');
if(this.value!='' && this.value!=null){
this.value = this.value;
}
});
$(a).off('change').on('change',function(){
if(this.value!='' && this.value!=null){
strLen = this.value.length;
if(strLen>len){
this.value = this.value.substr(0,len);
}else{
this.value = this.value;
}
}else{
this.value = '0.';
for(var i=0; i<digit; i++){
this.value += '0';
}
}
});
}
5、限制input只能输入正数
/**
* 限制input只能输入正数
* @param {[type]} a [description]
* @param {[type]} decimal [description]
* @return {[type]} [description]
*/
function inputPlusNum(a,decimal){
if(decimal==undefined){
decimal = '0,';
}
var reg = new RegExp("(\\d+)(\\.?)(\\d{"+decimal+"}).*$");
$(a).off('keyup').on('keyup',function(){
this.value=this.value.replace(/[^\d.]/g,''); //清除“数字”和“.”以外的字符
this.value=this.value.replace('.','$#$').replace(/\./g,'').replace("$#$","."); //只保留第一个. 清除多余的
this.value=this.value.replace(reg,'$1$2$3');
if(this.value.indexOf(".")< 0 && this.value!='' && this.value!=null){
this.value = parseFloat(this.value);
}
});
$(a).off('paste').on('paste',function(){
this.value=this.value.replace(/[^\d.]/g,''); //清除“数字”和“.”以外的字符
this.value=this.value.replace('.','$#$').replace(/\./g,'').replace("$#$","."); //只保留第一个. 清除多余的
this.value=this.value.replace(reg,'$1$2$3');
if(this.value.indexOf(".")< 0 && this.value!='' && this.value!=null){
this.value = parseFloat(this.value);
}
});
$(a).off('change').on('change',function(){
this.value=this.value.replace(/[^\d.]/g,''); //清除“数字”和“.”以外的字符
this.value=this.value.replace('.','$#$').replace(/\./g,'').replace("$#$","."); //只保留第一个. 清除多余的
this.value=this.value.replace(reg,'$1$2$3');
if(this.value.indexOf(".")< 0 && this.value!='' && this.value!=null){
this.value = parseFloat(this.value);
}
});
}
6、限制有 data-maxlen 该属性的输入框的可输入长度
/**
* 限制有 data-maxlen 该属性的输入框的可输入长度,例如:
* <input type="text" class="f-ctl name" placeholder="请输入姓名" data-maxlen="3" data-type="num">
* @returns
*/
function inputMaxLen(){
var maxLen,reg;
var strLen = 0;
// var regTest = new RegExp("(\\d+)(\\.?)(\\d{"+decimal+"}).*$");
$('[data-maxlen]').off('keyup').on('keyup',function(){
maxLen = $(this).data('maxlen');
if(!!$(this).data('type')){
if($(this).data('type')=='num'){
reg = new RegExp("^(\\d{1,"+maxLen+"})");
}else{
reg = new RegExp("^(.{1,"+maxLen+"})");
}
}else{
reg = new RegExp("^(.{1,"+maxLen+"})");
}
this.value=this.value.replace(reg,'$1');
if(this.value!='' && this.value!=null){
this.value = this.value;
}
});
$('[data-maxlen]').off('paste').on('paste',function(){
maxLen = $(this).data('maxlen');
if(!!$(this).data('type')){
if($(this).data('type')=='num'){
reg = new RegExp("^(\\d{1,"+maxLen+"})");
}else{
reg = new RegExp("^(.{1,"+maxLen+"})");
}
}else{
reg = new RegExp("^(.{1,"+maxLen+"})");
}
this.value=this.value.replace(reg,'$1');
if(this.value!='' && this.value!=null){
this.value = this.value;
}
});
$('[data-maxlen]').off('change').on('change',function(){
maxLen = $(this).data('maxlen');
if(!!$(this).data('type')){
if($(this).data('type')=='num'){
reg = new RegExp("^(\\d{1,"+maxLen+"})");
}else{
reg = new RegExp("^(.{1,"+maxLen+"})");
}
}else{
reg = new RegExp("^(.{1,"+maxLen+"})");
}
if(this.value!='' && this.value!=null){
strLen = this.value.length;
if(strLen>maxLen){
this.value = this.value.substr(0,maxLen);
}else{
this.value = this.value;
}
}else{
this.value = this.value;
}
});
}