Asp.net textbox in Android phone with numeric keyboard selected by default

烈酒焚心 提交于 2019-12-08 08:19:36

问题


We are developing a regular asp.net website (no Textviews), for one of the text boxes I need the textbox to open a numeric keyboard by default, is there a way to achieve this?

Below code works perfectly for iPhone / iPad but does not default to numeric keyboard for Android.

<asp:TextBox runat="server" type="number" pattern="\d*" onKeypress="if(event.keyCode < 48 || event.keyCode > 57){return false;}" />

Adroid version : 2.2.1

EDIT : When I use an HTML input box with type="number" it works perfectly. I need a way to use asp.Net textbox and see the numeric keyboard.


回答1:


Here is my solution:

<div style="display:none"><asp:TextBox ID="txt_Phone" runat="server" /></div>
<input onblur="document.getElementById('<%=txt_Phone.ClientID %>').value = this.value" type="tel" />

Enjoy :)




回答2:


Here's a slightly more elegant way:

PhoneBox.Attributes.Add("type", "tel");

This way there is only one input control and it doesn't mess up client-side validation. Also it makes it easy to assign this attribute to multiple controls (I just added a "keypad" class to the controls that I want it to apply to, then use a recursive control search method to find them all and add the attribute).



来源:https://stackoverflow.com/questions/6765990/asp-net-textbox-in-android-phone-with-numeric-keyboard-selected-by-default

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!