Click here to Skip to main content
14,735,478 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi

I would like to validate this text box in integer. What can I do?
<asp:TextBox ID="txt_postal_code" Width="85%" CssClass="tb_dbi" runat="server">

Thanks in advance.
Posted
Updated 18-Nov-11 2:58am
v3

Use Compare Validator
<asp:textbox id="textbox11" runat="server" xmlns:asp="#unknown">
<asp:comparevalidator id="intValidator" runat="server" controltovalidate="textbox1" operator="DataTypeCheck" type="int" errormessage="Value must be int" xmlns:asp="#unknown" />
   
v2
You have 2 choices, Use ASP.NET Validation Control, or use Javascript. both are displayed below.

1) ASP.NET validator controls:

Validator Controls in ASP.NET[^]

http://onetidbit.wordpress.com/2008/01/25/14/[^]

2) Javascript:

http://www.c-sharpcorner.com/UploadFile/purankaushal/103222006013805AM/1.aspx[^]

http://stackoverflow.com/questions/18082/validate-numbers-in-javascript-isnumeric[^]

Cheers :)
   
Hi afsal,
Numeric Textbox
   
<script language="javascript" type="text/javascript">
function isNumberKey(evt)
     {
        var charCode = (evt.which) ? evt.which : event.keyCode
        if (charCode > 31 && (charCode < 48 || charCode > 57))
        {
        if (charCode != 46)
        {
        alert("only Numbers are allowed");
           return false;

           }
        }
        return true;
     }
  </script>

txt_postal_code.Attributes["onkeypress"] = "javascript:return isNumberKey(event);";

Or use Compare Validator
<asp:TextBox ID="txt_postal_code" runat="server">
   <asp:CompareValidator ID="intValidator" runat="server" ControlToValidate="txt_postal_code" Operator="DataTypeCheck" Type="Integer" ErrorMessage="Value must be int">
   
v2
<HTML>
   <HEAD>
   <SCRIPT language=Javascript>
   
      function isNumberKey(evt)
      {
         var charCode = (evt.which) ? evt.which : event.keyCode
         if (charCode > 31 && (charCode < 48 || charCode > 57))
            return false;

         return true;
      }
     
   </SCRIPT>
   </HEAD>
   <BODY>
      <INPUT id="txtChar"  önkeypress="return isNumberKey(event)" type="text" name="txtChar">
   </BODY>
</HTML>


i think this will be helpful
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900