在Android上的输入字段中限制用户输入期间 [英] Restrict the user entering period in input field on Android

查看:217
本文介绍了在Android上的输入字段中限制用户输入期间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想限制用户在Android设备上的数字字段中输入特殊字符。 。 (点)字符不是按预期在onkeypress事件中传递值。



使用< input type =text>



但是使用< input type =number可以阻止用户输入任何特殊字符。 /> 我无法限制用户输入点键。



尝试了许多堆栈溢出帖子。但仍然没有运气。



下面是我想尝试的代码:



HTML

 < input type =numberng-model =registrationField.mobilenumbertabindex =0no-special -character =^ [0-9  - ] * $> 

JS

  app.directive(noSpecialCharacter,[function(){
return {
restrict:A,
link:function elem,attrs){
var limit = parseInt(attrs.limitTo);
var regex = RegExp(attrs.noSpecialCharacter);
angular.element(elem).on function(e){
if(e.key ==Unidentified){
e.preventDefault();
return false;
}
}
}
}
}])

code> keydown 事件,因为 keypress 事件在数字键盘中按下点键不会触发。



此外,当用户输入点时,我收到 e.key 未识别键。所以我试图用它来操作。



我已经尝试修剪最后一个值,当用户输入点键,也不工作。



尝试了 Android Nexus 5X中的上述代码装置。

解决方案

编辑:正确地,这个解决方案不会在移动设备上工作,这是输入字段的纯javascript解决方案。

 < div& 
< input onkeypress ='return event.charCode> = 48&&& event.charCode< = 57'type =numberplaceholder =输入值/>
< / div>

这里是工作jsfiddle
http://jsfiddle.net/tZPg4/16784/


I want to restrict the user from entering special character in a number field on an Android device. The "." (dot) character is not passing values in an "onkeypress" event, as expected.

With <input type="text"> I can able to block the user from entering any special character.

But with <input type="number"/> I am not able to restrict the user from entering dot key.

Tried with many stack overflow posts. But still no luck.

Below is the code i am trying:

HTML

<input type="number" ng-model="registrationField.mobilenumber" tabindex="0" no-special-character="^[0-9-]*$" >

JS

app.directive("noSpecialCharacter", [function() {
    return {
        restrict: "A",
        link: function(scope, elem, attrs) {
            var limit = parseInt(attrs.limitTo);
            var regex = RegExp(attrs.noSpecialCharacter);
            angular.element(elem).on("keydown", function(e) {
                if(e.key == "Unidentified"){
                    e.preventDefault();
                    return false;
                }
            });
        }
    }
}])

I am using keydown event because keypress event is not triggering on press of dot key in number keypad.

Also, I am receiving the e.key as Unidentified when user has entered the dot key. So i am trying to manipulate with it.

I have tried with trimming the last value when the user has entered the dot key and that is also not working.

Please suggest any possible way.

Tried the above code in Android Nexus 5X device.

解决方案

Edit: I didn't understood question correctly, this solution will not work on mobile devices, this is plain javascript solution for input field.

<div>
<input onkeypress='return event.charCode >= 48 && event.charCode <= 57' type="number" placeholder="Input Value" />
</div>

Here is working jsfiddle http://jsfiddle.net/tZPg4/16784/

这篇关于在Android上的输入字段中限制用户输入期间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆