开发者

Not allowing alphabets or other symbols than numerics (0-9) or decimal in Android browser

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?


maybe it is helpful to add android:numeric="integer" to the xml?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜