Robin Schambach
Robin Schambach

Reputation: 846

asp.net Textbox Textmode Number, allow numbers only

I would just like to know if there is a way in ASP.NET to allow only numbers in textboxes with textmode="number"

when I use this:

<asp:TextBox runat="server" TextMode="Number" ID="TextBoxDuration" Width="250"></asp:TextBox>
<asp:RequiredFieldValidator ControlToValidate="TextBoxDuration" runat="server" ErrorMessage="Dieses Feld darf nicht leer sein" /><br />
<asp:RegularExpressionValidator runat="server" ControlToValidate="TextBoxDuration" validationexpression="((\d+)((\.\d{1})?))$" ErrorMessage="Nur Zahlen" />

users can still input the char e and +, -

I don't like to use normal Textbox with the same Regularexpressionvalidator (which actually would work)

example:

enter image description here

Upvotes: 8

Views: 56215

Answers (9)

codeMonkey
codeMonkey

Reputation: 4845

In the code-behind, just set

this.TextBoxDuration.Attributes["type"] = "number";

Upvotes: 0

Jineesh Uvantavida
Jineesh Uvantavida

Reputation: 661

You can simply use regular expression validator as

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1"
    ControlToValidate="TextBox1" runat="server"
    ErrorMessage="Only Numbers allowed"
    ValidationExpression="\d+">

</asp:RegularExpressionValidator>

Also you can use the below regular expression to make the text-field as 12 digit number to be added.(madatory)

^[0-9]{12}$

Also you can make the field as between 10 and 12 digits (mandatory) as below

^[0-9]{10-12}$

Upvotes: 6

nirmalVaishnav
nirmalVaishnav

Reputation: 21

you can use filter textbox extender. it will allows only number. there is no negative numbers or e,+,- keys

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

Upvotes: 1

Developer
Developer

Reputation: 245

Adding Type="number" will work.

<asp:TextBox ID="txtQty"  type="number" runat="server"></asp:TextBox>

Upvotes: 0

shahid zaman
shahid zaman

Reputation: 122

You can also use asp.net field validate like this

<asp:TextBox runat="server" CssClass="form-control"  ID="txtNumero"  />
                                                <asp:RegularExpressionValidator
                                                    ID="txtNumero"
                                                    ControlToValidate="EmailTextBox"
                                                    Text="(Invalid number)"
                                                    ForeColor="Red"
                                                    ValidationExpression="^\d+$"
                                                    runat="server" /> 

Upvotes: 0

Manish Goswami
Manish Goswami

Reputation: 875

REGEX

^[0-9]*$


<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="tbAccount"
    ErrorMessage="Please Enter Only Numbers" ForeColor="Red" ValidationExpression="^\d+$">        </asp:RegularExpressionValidator>

IF YOU DON'T WANT USER TO TYPE OTHER CHARACTER ON KEY EVENT THEN

$('#TEXTBOXID').keypress(function (e) {
    var a = [];
    var k = e.which;

    for (i = 48; i < 58; i++)
    a.push(i);

    // allow a max of 1 decimal point to be entered

    if (!(a.indexOf(k) >= 0)) e.preventDefault();


});

Upvotes: 1

shahid zaman
shahid zaman

Reputation: 122

You can use jQuery like this

Number : <input type="text" name="quantity" id="quantity" />&nbsp;<span id="errmsg"></span>

    <script>
        $(document).ready(function () {
            //called when key is pressed in textbox
            $("#quantity").keypress(function (e) {
                //if the letter is not digit then display error and don't type anything
                if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
                    //display error message
                    $("#errmsg").html("Digits Only").show().fadeOut("slow");
                    return false;
                }
            });
        });

    </script>

Upvotes: 2

MarcS
MarcS

Reputation: 691

you can make a Javascript function into your Textbox.

Try <asp:TextBox ID="txtNumero" Text="" onkeydown="return jsDecimals(event);" Width="75px" runat="server"></asp:TextBox>

Then

function jsDecimals(e) {

    var evt = (e) ? e : window.event;
    var key = (evt.keyCode) ? evt.keyCode : evt.which;
    if (key != null) {
        key = parseInt(key, 10);
        if ((key < 48 || key > 57) && (key < 96 || key > 105)) {
            //Here is where respond with 0 o 1.
            if (!jsIsUserFriendlyChar(key, "Decimals")) {
                return false;
            }
        }
        else {
            if (evt.shiftKey) {
                return false;
            }
        }
    }
    return true;
}

Upvotes: 2

Alper Şaldırak
Alper Şaldırak

Reputation: 1054

You can use RegularExpressionValidator for this. below is the sample code:

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" ControlToValidate="TextBox1" runat="server" ErrorMessage="Only Numbers allowed" ValidationExpression="\d+"></asp:RegularExpressionValidator>

above TextBox only allowed integer to be entered because in RegularExpressionValidator has field called ValidationExpression, which validate the TextBox. However, you can modify as per your requirement.

You can see more example in MVC and Jquery here.

Upvotes: 3

Related Questions