Naga
Naga

Reputation: 59

<asp:TextBox> should accept only numbers

I have created a textbox in asp as given below.Now my problem is Textbox should accept only numbers.

<asp:TextBox ID="txtRate" runat="server" Text="" Width="100%" TabIndex="6" ></asp:TextBox>

How can I implement the function in Js file and call it in asp file.I have tried keypress event but error was raising as "keypress not an attribute of asp textbox".

I have tried Rangevalidators,RegularExpressionValidator also even though I was unable to get the requirement.

Please help me with this..

Upvotes: 0

Views: 3213

Answers (7)

you should be use ajaxtoolkit to do that

 <ajaxToolkit:FilteredTextBoxExtender ValidChars="yourcharallow" runat="server" Enabled="True" TargetControlID="yourTextBox" ID="FilteredTextBoxExtender1"></ajaxToolkit:FilteredTextBoxExtender>

Upvotes: 0

Lokki
Lokki

Reputation: 382

This one should work:

    <script language="javascript" type="text/javascript">
        function CheckNumeric(e) {

            if (window.event) // IE 
            {
                if ((e.keyCode < 48 || e.keyCode > 57) & e.keyCode != 8) {
                    event.returnValue = false;
                    return false;

                }
            }
            else { // FireFox
                if ((e.which < 48 || e.which > 57) & e.which != 8) {
                    e.preventDefault();
                    return false;

                }
            }
        }

    </script>

    <asp:TextBox ID="txtRate" runat="server" Text="" Width="100%" TabIndex="6" onkeypress="CheckNumeric(event);"></asp:textbox>

source

Upvotes: 0

Brian Flaherty
Brian Flaherty

Reputation: 39

I used a RegularExpressionValidator in a project to only accept numbers and decimals.

<asp:RegularExpressionValidator ID="RepRateRegExVal" 
       ControlToValidate="repRateTxtbox" 
       ValidationExpression="^\d*\.?\d*$"
       runat="server" 
       Display="Dynamic" 
       CssClass="validator"
       Text="Repetition Rate must be a valid number">
</asp:RegularExpressionValidator>

If you want to only accept one place after the decimal change the expression to "^\d*.?\d$"

Upvotes: 0

sara
sara

Reputation: 3589

<asp:CompareValidator runat="server" ControlToValidate="txtRate" Type="Integer" Operator="DataTypeCheck" Text="Must be a number!"/>

Can swap out Integer for Double if needed.

Upvotes: 0

Jonas
Jonas

Reputation: 183

Use a rangevalidator or a regularexpressionvalidator. You have to set ValidationGroup and CausesValidation for the textbox.

See: http://www.tutorialspoint.com/asp.net/asp.net_validators.htm for some examples.

You could also do validation clientside with javascripts.

Upvotes: 0

Musakkhir Sayyed
Musakkhir Sayyed

Reputation: 7170

<asp:FilteredTextBoxExtender ID="TextBox1_FilteredTextBoxExtender" runat="server"
Enabled="True" TargetControlID="TextBox1" FilterType="Numbers">
</asp:FilteredTextBoxExtender>

Upvotes: 0

Tim Schmelter
Tim Schmelter

Reputation: 460058

You should use a CompareValidator with Operator set to DataTypeCheck:

<asp:CompareValidator runat="server" Operator="DataTypeCheck" Type="Double" 
 ControlToValidate="txtRate" ErrorMessage="Value must be a number" />

These are the types you can check:

  • String
  • Integer
  • Double
  • Date
  • Currency

Upvotes: 1

Related Questions