Android 浏览器中不允许使用字母或除数字 (0-9) 或小数之外的其他符号
以下代码适用于我: firefox
、IE 8
、chrome
、Safari
和 iphone.
function dotplaced(myfield){
if(myfield.indexOf(".")===-1){
return false;
}
return true;
}
function NumbersOnly(myfield, e) {
var key;
var keychar;
if (window.event) {
key = window.event.keyCode;
}
else if (e) {
key = e.which;
}
else {
return true;
}
keychar = String.fromCharCode(key);
// control keys
if ((key == null) || (key == 0) || (key == 8) ||
(key == 9) || (key == 13) || (key == 27)) {
return true;
}
// numbers
else if ((("0123456789").indexOf(keychar) > -1)) {
return true;
}
// decimal point jump
else if (!dotplaced(myfield.value) && (keychar == ".")) {
//myfield.form.elements[dec].focus();
return true;
}
else {
return false;
}
}
而 Android 2.1 则给出了一些错误。如果我在文本框中没有任何内容,Android 也允许我输入字母,如果我输入一些默认值(例如 7)。然后,它会受到限制,直到我输入句号或删除文本框的所有文本。有人有吗关于这个的想法?
Following code worked for me on : firefox
, IE 8
, chrome
, Safari
and iphone
.
function dotplaced(myfield){
if(myfield.indexOf(".")===-1){
return false;
}
return true;
}
function NumbersOnly(myfield, e) {
var key;
var keychar;
if (window.event) {
key = window.event.keyCode;
}
else if (e) {
key = e.which;
}
else {
return true;
}
keychar = String.fromCharCode(key);
// control keys
if ((key == null) || (key == 0) || (key == 8) ||
(key == 9) || (key == 13) || (key == 27)) {
return true;
}
// numbers
else if ((("0123456789").indexOf(keychar) > -1)) {
return true;
}
// decimal point jump
else if (!dotplaced(myfield.value) && (keychar == ".")) {
//myfield.form.elements[dec].focus();
return true;
}
else {
return false;
}
}
While Android 2.1 is giving some errors. If i dont have anything in textbox, android is allowing me to enter alphabets too, if i enter some default value say 7. Then, it is restricting until i enter a period sign or i delete all the text of textbox.Does anyone have any idea regarding this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许将
android:numeric="integer"
添加到 xml 会有所帮助?maybe it is helpful to add
android:numeric="integer"
to the xml?当我对此进行更多研究时。然后我才知道 Android 的自动文本预测行为是问题的原因。
When i did more research on this. Then i came to know that it is Android's behaviour of autotext predictions that is the cause of problem.