input text box input limiting

1. The text box can only enter a positive number, and up to two decimal places:

oninput = "var p2 = parseFloat(value).toFixed(2);value = p2>=0?(/\.0?$/.test(value)?value:p2.replace(/0$/,'').replace(/\.0$/,'')):''"

2. The text box can only enter positive numbers:

oninput = "value=value.replace(/[^\d]/g,'')"

3. Text boxes can only enter a number from 0 to 30:

oninput = "testNum(this);"

/**限制只能输入0-30的数字*/
function testNum(num) {
	var p = /^([0-9]|2[0-9]|30)$/;
	if(!p.test(num.value)){
		num.value = "";
	}
}

4. The text box can only enter negative numbers and fractional (fractional input 2 only)

oninput = "upperCase(this);"
/**限制用户只能输入正负数与小数(小数仅能输入2位)*/
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));
	}
}

At present the project to use, the latter will continue to add.

Published 14 original articles · won praise 2 · Views 791

Guess you like

Origin blog.csdn.net/breakaway_01/article/details/103783452