可以使html输入类型号仅显示5的倍数

时间:2015-06-03 02:31:11

标签: html input

是否可以使html输入类型编号仅显示5的倍数(5,10,15 ....)?

myCircle.on("dblclick", function () {
    //my dblclick stuff
    return false;
});

3 个答案:

答案 0 :(得分:9)

仅限HTML 5

<input type="number" name="pax" step="5">

还有其他javascript或正则表达式解决方案,但它们不是必需的。

进一步阅读类型输入: http://www.w3schools.com/html/html_form_input_types.asp

以下是使用一些更受欢迎的数字输入属性的示例。

<input type="number" name="pax" min="0" max="100" step="5" value="0">

答案 1 :(得分:0)

一种方法是使用onchange函数将数字元素的id作为参数,并将元素设置为当前输入的舍入到最接近的5。

function update(id){
  var value = document.getElementById(id).value;
  value = parseInt(math.round(value/5)*5);
}

可以实现如下:

<input type="number" onchange="update(this.id)" id="number-thing" name="pax" step="5">

我不知道直接的方法是只显示倍数,包括输入的输入。对于数字,有step属性(如上所示),可将数字更改为步骤的下一个最高或最低倍数。

此解决方案解决了递增和递减按钮,以及取决于自定义用户输入的五步。

答案 2 :(得分:0)

请使用JavaScript语言{$ 3}}查看我的网页上文本输入元素值的跨浏览器过滤器项目。您可以将值过滤为整数,浮点数或编写自定义过滤器。 html输入类型编号的代码示例仅显示5的倍数:

<!doctype html>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>Input Key Filter Test</title>
	<meta name="author" content="Andrej Hristoliubov anhr@mail.ru">
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
	
	<!-- For compatibility of IE browser with audio element in the beep() function.
	https://www.modern.ie/en-us/performance/how-to-use-x-ua-compatible -->
	<meta http-equiv="X-UA-Compatible" content="IE=9"/>
	
	<link rel="stylesheet" href="https://rawgit.com/anhr/InputKeyFilter/master/InputKeyFilter.css" type="text/css">		
	<script type="text/javascript" src="https://rawgit.com/anhr/InputKeyFilter/master/Common.js"></script>
	<script type="text/javascript" src="https://rawgit.com/anhr/InputKeyFilter/master/InputKeyFilter.js"></script>
	
</head>
<body>
	To make html input type number only show numbers which are multiple of 5 
<input id="MultipleOf5">
<script>
	function TextAddMultipleOf5(elementInput){
		inputKeyFilter.TextAdd(isRussian() ?
				"разрешается только числа, которые кратны 5. Например: 5 10 15"
				: "only allowed numbers which are multiple of 5. Examples: 5 10 15"
			, elementInput);
	}
	
	function isMultipleOf5(input){
		var integer = parseInt(input.value);
		if(inputKeyFilter.isNaN(integer, input))
			return false;//value is not integer
		if(parseInt(integer/5)*5 != integer)
			return false;//integer is not multiple of 5
		return true;//integer is multiple of 5
	}
	
	inputKeyFilter.Create("MultipleOf5"
		, function(event){//onChange event
			inputKeyFilter.RemoveMyTooltip();
			
			var elementNewMultipleOf5 = document.getElementById("NewMultipleOf5");
			if(isMultipleOf5(this)){
				elementNewMultipleOf5.innerHTML = this.value;
				return;
			}
			elementNewMultipleOf5.innerHTML = "";
			TextAddMultipleOf5(this);
		}
		, function(elementInput, value){//customFilter
			//if(value.match(/^(-?\d*)$/) != null)//value is positive and negative number
			if(value.match(/^(\d*)$/) != null)//value is positive number only
				return true;//value is number
			TextAddMultipleOf5(elementInput);
			return false;//invalid value
		}
		
		//onblur event. Use this function if you want set focus to the input element again if input value is NaN. (empty or invalid)
		, function(event){
			var value;
			if(isMultipleOf5(this))
				value = this.value;
			if(inputKeyFilter.isNaN(parseInt(value), this))
				TextAddMultipleOf5(this);
		}
	);
</script>
 New, multiple of 5 number: <span id="NewMultipleOf5"></span>
</body>
</html>

另见我的页面Input Key Filter