Don't allow typing alphabetic characters in a <input type=number />

前端 未结 9 736
情话喂你
情话喂你 2020-12-30 01:13

I need to have a textbox, where, whenever typed inside should ONLY allow numbers [0-9]. I\'ve used type=\"number\" which definitely holds the client side valida

9条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2020-12-30 01:49

    input type="number" for getting Numeric Keyboard on Mobile Device

    Java-Script

    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;
    
                // only one decimal point
                else if ((keychar == "."))
                {
                    if (myfield.value.indexOf(keychar) > -1)
                        return false;
                }
                else
                    return false;
            }
    

    Html

    
    

提交回复
热议问题