本文介绍了如何通过asp.net中的javascript限制用户仅在TextBox中输入alphabates的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过asp.net中的javascript限制用户仅在TextBox中输入alphabates.
仅Alphabates(a,b,c,d,e,f,g,h,i,............. z).
和(A,B,C,D,E,F,G,H,I,............ Z).

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

推荐答案

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();

        }


<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


var text=document.getElementById("your id");
if (!text.value.match(/^[a-zA-Z]+


这篇关于如何通过asp.net中的javascript限制用户仅在TextBox中输入alphabates的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 20:35