显示仅允许数字和小数点的输入?

时间:2012-02-09 02:51:46

标签: iphone html html5 input cordova

有没有办法定义<input>,它显示的键盘输入只允许数字和小数点(或国际用户的逗号)?

<input type='tel'>显示我不需要的电话垃圾,没有小数点
<input type='number' pattern='[0-9]*'>显示所有数字,但没有小数点

我可以做些什么来获得我需要的输入?

4 个答案:

答案 0 :(得分:4)

您可以在type =“number”字段中尝试此属性:

pattern="\d+(\.\d*)?"

这将只允许带/不带小数点的数字和右边的浮点数。

答案 1 :(得分:1)

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

<!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>
	<h1>Float field</h1>
Float field: 
<input id="Float"
	onchange="javascript: onChangeFloat(this)"
	onblur="inputKeyFilter.isNaN(parseFloat(this.value), this);"
/>
<script>
	CreateFloatFilter("Float");
	
	function onChangeFloat(input){
		inputKeyFilter.RemoveMyTooltip();
		var elementNewFloat = document.getElementById("NewFloat");
		var float = inputKeyFilter.parseFloat(input.value);
		if(inputKeyFilter.isNaN(float, input)){
			elementNewFloat.innerHTML = "";
			return;
		}
		elementNewFloat.innerHTML = float + " or localized value: " + float.toLocaleString();
	}
</script>
 New float: <span id="NewFloat"></span>
</body>
</html>

另见我的页面Input Key Filter

答案 2 :(得分:0)

如果您是在真正的iOS应用程序中执行此操作,您可以在键盘顶部添加自己的按钮,基于this question,这是关于附件视图但相同的过程可以工作。

否则,IamChuckB是正确的,five keyboard types given in the Apple Developer's Library是详尽无遗的。

答案 3 :(得分:0)

=&GT;它只允许单个小数点(。)

=&GT;点后面只允许三位十进制值。您可以通过将{1,3}更改为以下RegEx来进行修改。

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string  {

        if (!string.length)
            return YES;

        NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
        NSString *expression = @"^([0-9]+)?(\\.([0-9]{1,3})?)?$";
        NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:expression
                                                                               options:NSRegularExpressionCaseInsensitive
                                                                                 error:nil];
        NSUInteger numberOfMatches = [regex numberOfMatchesInString:newString
                                                            options:0
                                                              range:NSMakeRange(0, [newString length])];
        if (numberOfMatches == 0)
            return NO;


        return YES;

    }
相关问题