Click here to Skip to main content
15,889,403 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi friends,
i have a Textbox like Add. Tax, in this textbox value can be like 12 or 12.5.
the textbox should contain such values not others any (special characters or alphabets).
how it can be achieve.

Regards,
aamir
Posted

Following code accepts numbers and a decimal point.

C#
function isNumberKey(evt)
           {
               var charCode = (evt.which) ? evt.which : event.keyCode

               if (charCode == 46)
               {
                   var inputValue = $("#inputfield").val()
                   if (inputValue.indexOf('.') < 1)
                   {
                       return true;
                   }
                   return false;
               }
               if (charCode != 46 && charCode > 31 && (charCode < 48 || charCode > 57))
               {
                   return false;
               }
               return true;
           }
 
Share this answer
 
C#
function validatenumerics(key) {
    //getting key code of pressed key
    var keycode = (key.which) ? key.which : key.keyCode;
    //comparing pressed keycodes

    if (keycode > 31 && (keycode < 48 || keycode > 57) && keycode != 46) {
        alert(" You can enter only characters 0 to 9 ");
        return false;
    }
    else return true;
}
function isNumberKey(key) {
    //getting key code of pressed key
    var keycode = (key.which) ? key.which : key.keyCode;
    //comparing pressed keycodes

    if (keycode > 31 && (keycode < 48 || keycode > 57) && keycode != 47) {
        alert(" You can enter only characters 0 to 9 ");
        return false;
    }
    else return true;
}

here first method for "Textbox" Accept only 0-9 numbers
second method for "textbox accept 0-9 and one decimal point"

You can place the code between Script tag..

call like this in text box tag..
onkeypress="return isNumberKey(event)"
 
Share this answer
 
Go for the javascript coding
Its better and light weight
check the below code
HTML
<html>
  <head>
    <script language="Javascript">
       <!--
       function isNumberKey(evt)
       {
          var charCode = (evt.which) ? evt.which : event.keyCode
          if (charCode != 46 && charCode > 31 
            && (charCode < 48 || charCode > 57))
             return false;

          return true;
       }
       //-->
    </script>
  </head>
  <body>
    <input id="txtChar" onkeypress="return isNumberKey(event)">
           type="text" name="txtChar"&gt;
  </input></body>
</html>
 
Share this answer
 
v2
Comments
aamir07 21-Jul-12 7:48am    
dear Ganesh,
thanks for ur reply,
i want to say that above javascript code accepts only Numbers not decimal values(12.5,5.5). i need the textbox should acepts numbers and decimal values as said.
How you do it depends on what you are producing: For web applications you could use a validator:
XML
<asp:TextBox ID="NumericTextBox" runat="server"></asp:TextBox>
    <asp:RegularExpressionValidator ID="NumericOnlyValidator" runat="server" ControlToValidate="TextBox1" ErrorMessage="Enter a valid number" ValidationExpression="^\d$"></asp:RegularExpressionValidator>

Or, you could do it with Javascript, and prevent entry of non-numerics:
function isNumeric(evt)
{
 var c = (evt.which) ? evt.which : event.keyCode
 if ((c >= '0'  && c <= '9') || c == '.' )
    return true;
 return false;
}


MyTextBox.Attributes.Add("onkeypress","return isNumeric(event)");
For a Winforms, I would use a NumericUpDown.
 
Share this answer
 
Comments
aamir07 21-Jul-12 11:12am    
Dear originalGriff,
i dont want to prevent numericl value.
the textbox should accept numbers aand numerics
example, 12/12.5,5/5.5
OriginalGriff 21-Jul-12 11:15am    
The code allows the user to press the buttons '0' to '9', and '.'
Which is what numeric values are made up of...a sequence of keys from the user.
Kenneth Haugland 24-Jul-12 16:03pm    
That is true, but Its not stopping the user from writing : 5.5.5.5..5.5.5.5.5.5, so you should proberbly add some check that only allows just one decimal separator.
This is Working greate ...........

event : onfocusout="fnAdddecimal('txtDTI');"

function fnAdddecimal(obj) {

txt = document.getElementById(obj);
var val = jsTrimValues(txt.value);
if (val != '') {
val = val.split('.00');
if (val.length > 1) {

}
else {
txt.value = txt.value + '.00';
}
}
}
 
Share this answer
 
simply you can use ajax tools

C#
<asp:textbox id="txtbox" runat="server" xmlns:asp="#unknown"></asp:textbox><asp:filteredtextboxextender targetcontrolid="txtbox" xmlns:asp="#unknown">
                     FilterType="Custom, Numbers" ValidChars="."  ID="FilteredTextBoxExtender1" runat="server">
                   </asp:filteredtextboxextender>



Filtered Texbox
 
Share this answer
 

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