jquery trapping allow only letters

会有一股神秘感。 提交于 2019-12-12 20:31:24

问题


I am creating a web page where I have a input text field in which I want to allow only alphabet..

How can i make it using jQuery??

Thanks


回答1:


jquery.inputmask is a jquery plugin which create an input mask. That is allow only input according to a specific pattern. e.g, date, text, numbers zipcode etc.

there is also another (older) version:

It allows a user to more easily enter fixed width input where you would like them to enter the data in a certain format (dates,phone numbers, etc). It has been tested on Internet Explorer 6/7, Firefox 1.5/2/3, Safari, Opera, and Chrome.A mask is defined by a format made up of mask literals and mask definitions. Any character not in the definitions list below is considered a mask literal. Mask literals will be automatically entered for the user as they type and will not be able to be removed by the user.




回答2:


See here

We bind to input change then take a look at the current string. We walk over the string and append any alphabetical characters to the output variable and return that.

$("#textField").bind("input", function(event) {
   var out = "";
   var str = this.value;
    for (var i = 0; i < str.length; i++) {
        if (/[A-Za-z]/.test(str.charAt(i))) {
            out = out.concat(str.charAt(i));    
        }
    }
    this.value = out;
 });


来源:https://stackoverflow.com/questions/4827535/jquery-trapping-allow-only-letters

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!