Render ASP.NET TextBox as HTML5 Input type "Number"

Curt picture Curt · Feb 22, 2012 · Viewed 60.4k times · Source

When an ASP.NET TextBox renders it produces:

<input type="text" />

However I need it to render as a HTML5 number type instead, like:

<input type="number" />

Is this possible?

Answer

zacharydl picture zacharydl · Aug 17, 2013

I had the same requirement for a mobile website using ASP.NET. After finding no good solution, I tried simply setting type="number" directly on the textbox. To my surprise, it worked! Incredulous, I created a simple test project to double-check. I ran this line of code in each .NET version:

<!-- this HTML tested in each .NET version -->
<asp:TextBox runat="server" type="number" />

Here are the results:

<!-- ASP.NET 2.0, 3.0, and 3.5 -->
<input name="ctl01" type="text" type="number" />

<!-- ASP.NET 4.0 and 4.5 -->
<input name="ctl01" type="number" />

Bottom line: if you are using ASP.NET 4.0 or newer, just add type="number" to your textbox.