jteixeira Posted April 17, 2007 Share Posted April 17, 2007 hi, I have a textbox and i want that the textbox only accepts int values. I'm working with visual web developer 2005 in c# Anyone can help me? Link to comment Share on other sites More sharing options...
aspnetguy Posted April 17, 2007 Share Posted April 17, 2007 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 More sharing options...
jteixeira Posted April 17, 2007 Author Share Posted April 17, 2007 it dosen't work Link to comment Share on other sites More sharing options...
aspnetguy Posted April 17, 2007 Share Posted April 17, 2007 how does it not work? do you get any error messages?Post both your .aspx file and your .aspx.cs or .aspx.vb file Link to comment Share on other sites More sharing options...
jteixeira Posted April 17, 2007 Author Share Posted April 17, 2007 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 More sharing options...
aspnetguy Posted April 17, 2007 Share Posted April 17, 2007 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 More sharing options...
jteixeira Posted April 17, 2007 Author Share Posted April 17, 2007 ok, thanks Link to comment Share on other sites More sharing options...
jesh Posted April 17, 2007 Share Posted April 17, 2007 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now