Jump to content

Numeric Textbox


jteixeira

Recommended Posts

you can use javascriptpage.aspx

<html><head>...<script type="text/javascript">  function isNumeric(box)  {	if(isNaN(box.value))	{	   alert("The value must be numeric!");	   box.focus();	}  }</script>...</head><body>...<asp:TextBox id="txtNumeric" runat="server"/>...</body></html>

page.aspx.cs

//in Page_LoadtxtNumeric.Attributes.Add("onblur","isNumeric(this)");

Link to comment
Share on other sites

using System;using System.Data;using System.Configuration;using System.Collections;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;public partial class Default2 : System.Web.UI.Page{	protected void Page_Load(object sender, EventArgs e)	{		txtNumeric.Attributes.Add("onblur", "isNumeric(this)");  	}	protected void TextBox1_TextChanged(object sender, EventArgs e)	{			}}

and

<html xmlns="http://www.w3.org/1999/xhtml" ><head runat="server">	<title>Untitled Page</title><script type="text/javascript"> function isNumeric(box) {  if(isNaN(box.value))  {   alert("The value must be numeric!");   box.focus();  } }</script></head><body>	<form id="form1" runat="server">	<div>	   <asp:TextBox id="txtNumeric" runat="server"/>	</div>	</form></body></html>

There is no errors

Link to comment
Share on other sites

it works fine. You have to click away from the box (that is what onblur means) to get the event to fire. You could use onchange instead of on blur. It would check everytime the value change (every keypress).

Link to comment
Share on other sites

For future reference, you can also use a RegularExpressionValidator:

<asp:RegularExpressionValidator ID="v_txtNumeric" runat="server" ControlToValidate="txtNumeric" ValidationExpression="[0-9]*" ErrorMessage="The value must be numeric!" /><asp:TextBox id="txtNumeric" runat="server"/>

Or a CompareValidator:

<asp:CompareValidator ID="v_txtNumeric" runat="server" ControlToValidate="txtNumeric" Operator="DataTypeCheck" Type="Integer" ErrorMessage="The value must be numeric!" /><asp:TextBox id="txtNumeric" runat="server"/>

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...