Click here to Skip to main content
15,901,205 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
How to Restrict user to enter only alphabates in TextBox by javascript in asp.net.
Alphabates only(a,b,c,d,e,f,g,h,i,.............z).
and (A,B,C,D,E,F,G,H,I,............Z).
Posted

XML
function AplhabetsOnly(var1) {
            var num = document.getElementById(var1).value;
            var letter= 0;
            for (n = 0; n < num.length; n++) {
                letter= num.charCodeAt(n) >= 65 && num.charCodeAt(n) <= 90 || num.charCodeAt(n) == 97 || num.charCodeAt(n) == 122;
                if (!letter) {
                    alert('Enter alphabets Only');

                }
            }
            document.getElementById('<%=txtMobile.ClientID %>').value = "";
            document.getElementById(var1).focus();

        }
 
Share this answer
 
v3
Hi,

Here I tried some code for you

Try this

HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="Scripts/jquery-1.4.1-vsdoc.js" type="text/javascript"></script>
    <script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
    <script language ="javascript" >

        function chg() {
          
            if (parseInt(event.keyCode) > 64 ) {
                if (parseInt(event.keyCode) < 91) {
                 
                }
                else {
                    var str = new String();
                   
                    str = document.getElementById("txtmbno").value;
                    document.getElementById("txtmbno").value = str.substring(0, str.length - 1);
                   
                }
            }
            else {
                var str = new String();
                str = document.getElementById("txtmbno").value;
                document.getElementById("txtmbno").value = str.substring(0, str.length - 1);
              
            } 
          return true;
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
     <input type ="text" id="txtmbno"  onkeyup ="chg()" />
     

    </div>
    </form>
</body>
</html>

you can change this code for accepting only numeric also

All the Best
 
Share this answer
 
v2
Comments
rkthiyagarajan 27-Sep-11 8:09am    
Nice My5+..
var text=document.getElementById("your id");
if (!text.value.match(/^[a-zA-Z]+$/))
{
   alert("Please Enter only letters in text");
}
 
Share this answer
 
v2
Hi Shukla Pawan,

All of them gave you good example but you should know how that works!?.So you learn first? how.. Nothing just you revised your beginning computer class notes just glance the keyboard ASCII codes that is the main part of Restrict the particular keys in keyboard using JavaScript, then you got complete idea of making some task like this....


All the best
Happy coding..
 
Share this answer
 
XML
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="cc1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <div>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <cc1:FilteredTextBoxExtender ID="TextBox1_FilteredTextBoxExtender"
            runat="server" Enabled="True" TargetControlID="TextBox1"
            ValidChars="abcdefghijklmnopqrstuvwxyz">
        </cc1:FilteredTextBoxExtender>
    </div>
    </form>
</body>
</html>

ajax control toolkit select Filter textBox extender & set its Valid Char property...your problem has been solved
 
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