Click here to Skip to main content
15,905,566 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
Could you please tell me how the textbox accepts only Positive numbers


I don't want to show an Error Message..

whenever we enter negative value the textbox not accepting.


How we write textBox1_KeyPress event.
Posted
Updated 11-Jan-11 15:09pm
v2
Comments
Bryian Tan 11-Jan-11 21:16pm    
Hi, so do you want negative number or not?
thatraja 11-Jan-11 21:25pm    
You neutral-voted my answer?, the first revision of your question is "Hi Could you please tell me how the textbox accepts only Positive numbers", you didn't said about keypress event. It's not a fair thing. You should have asked your question clearly with full details. So that we can get a clear solution for you. Please don't let down the answerers. Here many people spending their valuable time here for helping other peoples. cheers.

How about using javascript code? add this js event

onkeypress=""return allowOnlyNumber(event);


then all you need is

C#
function allowOnlyNumber(evt)
{
  var charCode = (evt.which) ? evt.which : event.keyCode
  if (charCode > 31 && (charCode < 48 || charCode > 57))
    return false;
  return true;
}
 
Share this answer
 
Comments
venkatrao palepu 11-Jan-11 21:25pm    
Thank you so much

it's too helpful for me...
Sergey Alexandrovich Kryukov 11-Jan-11 21:57pm    
Yusuf, I think you forgot to allow backspace...
Yusuf 12-Jan-11 8:38am    
@SAKryukov. Oops! it was from our code base. In out functionality, we did not want backspace. I should have made a note about it. Sorry!
Google dude?

Use RegularExpressions
How To Allow Only Numbers In TextBox Control?[^]
 
Share this answer
 
Comments
venkatrao palepu 11-Jan-11 21:07pm    
Hi
it is correct.

But i am not expecting this

i don't want show an error message

whenever we are pressing negative value it's not accepting.

How we write in textBox1_KeyPress event.
XML
<b>Simple Way to enter only Positive Value in textbox</b>
<b>Example:</b>
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Public_Default" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
      <title></title>
</head>
<body>
      <form id="form1" runat="server">
      <asp:ScriptManager ID="script1" runat="server">
      </asp:ScriptManager>
      <div>
            <asp:TextBox ID="txtbox1" runat="server"></asp:TextBox>
            <cc1:FilteredTextBoxExtender id="ftbe" runat="server" targetcontrolid="txtbox1"
                  filtertype="Numbers" validchars="0123456987" />
      </div>
      </form>
</body>
</html>
<b>Note:Add AjaxControlToolKit in bin directory</b>
 
Share this answer
 
More exactly (you also need to allow backspace):

XML
<html>
    <head>
        <script type="text/javascript"><!--
            function filterDigits(eventInstance) {
                eventInstance = eventInstance || window.event;
                    key = eventInstance.keyCode || eventInstance.which;
                if ((47 < key) && (key < 58) || key == 8) {
                    return true;
                } else {
                        if (eventInstance.preventDefault) 
                             eventInstance.preventDefault();
                        eventInstance.returnValue = false;
                        return false;
                    } //if
            } //filterDigits
        --></script>
    </head>
<body>
<input type="text" onkeypress="filterDigits(event)"/>
</body>
</html>
 
Share this answer
 
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