Aman Ullah Zadran
Aman Ullah Zadran

Reputation: 11

How to set a text Box Input type="text" in asp.net?

How can I set input type to text only inside this text box?

I tried:

type="text";

remove toString() from cs

But they didn't work correctly.

<asp:TextBox ID="txtShort"  Width="200px"  value="abc"   runat="server" ValidationGroup="abc" MaxLength="10"></asp:TextBox>

.CS

'" + txtShort.Text.ToString().Trim() +

Upvotes: 0

Views: 1484

Answers (3)

Piyush Khanna
Piyush Khanna

Reputation: 306

If you want to restrict input of numbers in your textbox, you can also use client-side code - <asp:TextBox onkeydown="return !(event.keyCode>=48 && event.keyCode<=57);"></asp:TextBox>

You can include keycodes for numpad0 to 9 as well.

Upvotes: 1

Santhakumar Munuswamy
Santhakumar Munuswamy

Reputation: 548

you can use allow only alphabets using javascript method calling in the textbox with onkeypress event. you can try below code

   <%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication5._Default" %>

<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
    <script language="Javascript" type="text/javascript">
        function allowAlphabets(evt) {
            var charCode = (evt.which) ? evt.which : event.keyCode
            if ((charCode <= 90 && charCode >= 65) || (charCode <= 122 && charCode >= 97 || charCode==8)) {

                return true;
            }
            alert("Enter only Alphabets");
            return false;
        }
</script>
    <asp:TextBox ID="txtName" runat="server" onkeypress="return allowAlphabets(event)"></asp:TextBox>
</asp:Content>

Upvotes: 0

Deepak
Deepak

Reputation: 376

Assuming you only need alphabets inside textbox you can use a regular expression like :-

    <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="only characters allowed" ControlToValidate="txtShort" ValidationExpression="^[A-Za-z]*$" ></asp:RegularExpressionValidator>

Upvotes: 0

Related Questions