input只能输入数字,负数,小数(保留两位)

js:

function upperCase(obj){//用户只能输入正负数与小数
	if(isNaN(obj.value) && !/^-$/.test(obj.value)){
		obj.value="";
	}
	if(!/^[+-]?\d*\.{0,1}\d{0,1}$/.test(obj.value)){
		obj.value=obj.value.replace(/\.\d{2,}$/,obj.value.substr(obj.value.indexOf('.'),3));
	}
}

html:

<input type='text' onkeyup='upperCase(this)' name='imin1' id='imin1'/>

<!--限制文本框只能输入正数、小数、负数-->

<input type="text" onkeyup="this.value=this.value.replace(/[^\-?\d.]/g,'')"/>

附:https://funteas.com/topic/592f828e572bfb9c4d965ab0(关于正则的一些表达式,别人的)

猜你喜欢

转载自blog.csdn.net/weixin_41700532/article/details/80882523