问题
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