Click here to Skip to main content
15,890,825 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have 2 text boxes viz. txt_Long and txt_Lat. I want that only numeric values be able to be inserted into those text boxes and not numeric or alphanumeric values. If tried to insert any other values except numeric value, a validation violation message should be displayed in red color just under the respective text boxes.Can any one please help ?
Posted
Comments
Tom Marvolo Riddle 28-Jan-14 5:19am    
In Asp.net or Windowforms?
Member 10520129 29-Jan-14 1:26am    
asp.net
Tom Marvolo Riddle 29-Jan-14 1:30am    
Please check solution2.Try it and let me know

If you are using win forms

C#
public void txtdigitonly_KeyPress(KeyPressEventArgs e)
       {
           try
           {
               if(char.IsDigit(e.KeyChar) == true)
               {
                   e.Handled=false;
                   lblMessage.text="";
               }
               else
               {
                   e.Handled=true;
                   lblMessage.text="Please Insert Numeric Value";
               }
           }
           catch { }
       }



lblMessage is the label in which you want to display the message.
 
Share this answer
 
v2
using with AJAX tool:-

XML
<asp:TextBox ID="txtContactNo" runat="server" class="textfield" MaxLength="10"></asp:TextBox>
                                  <cc1:FilteredTextBoxExtender ID="FilteredTextBoxExtender2" runat="server" ValidChars="0123456789"
                                      TargetControlID="txtContactNo">
                                  </cc1:FilteredTextBoxExtender>


or

using with JavaScript:-

XML
<script type="text/javascript" language="javascript">
    function isNumberKey(evt) {
        var charCode = (evt.which) ? evt.which : event.keyCode
        if (charCode > 31 && (charCode < 48 || charCode > 57))
            return false;
        if (charCode == 8)
            return true;

        return true;
    }
   </script>

   <asp:TextBox ID="txtContactNo" onkeypress="return isNumberKey(event)" runat="server" class="textfield" MaxLength="10"></asp:TextBox>
 
Share this answer
 
v3
try this js function:-
C#
// To accept only Numeric Values 
function IsNumberKeyOnly(event) {
    var e = window.event || evt; // for trans-browser compatibility
    var charCode = e.which || e.keyCode;
    if ((charCode > 31) && (charCode < 48 || charCode > 57)) {
    {
      return false;
    }
    }
    if (charCode == 8) { return true; }
    return true;
}
 
Share this answer
 
Comments
Tom Marvolo Riddle 30-Jan-14 0:08am    
Good one +5
TrushnaK 30-Jan-14 0:09am    
thanks Jas24...
Tom Marvolo Riddle 30-Jan-14 0:15am    
You are most welcome!
For Asp.net use AjaxFilteredTextBoxExtender

eg:
C#
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                           
<asp:FilteredTextBoxExtender ID="FilteredTextBoxExtender3" runat="server" TargetControlID ="TextBox1" FilterType="Numbers">
</asp:FilteredTextBoxExtender>
 
Share this answer
 
v3
Comments
Tom Marvolo Riddle 29-Jan-14 1:36am    
This will allow only numbers in textbox
just validate....
use validation event for validate your box.
 
Share this answer
 
You could use jQuery or simple javascript in the onkey up event.
 
Share this answer
 

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

  Print Answers RSS


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