简体   繁体   中英

<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..

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
<asp:FilteredTextBoxExtender ID="TextBox1_FilteredTextBoxExtender" runat="server"
Enabled="True" TargetControlID="TextBox1" FilterType="Numbers">
</asp:FilteredTextBoxExtender>

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.

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

如果需要,可以将Integer换成Double

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$"

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

你应该使用ajaxtoolkit来做到这一点

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM