不允许使用空格作为第一个字符,并且只允许使用jquery的字母 [英] Not allow space as a first character and allow only letters using jquery
问题描述
我正在使用jquery进行名称验证,我尝试了下面给出的代码
Im using jquery for the name validation I have tried a code which is given below
$("#contactname").keypress(function(e) {
if(e.which < 97 /* a */ || e.which > 122 && (e.which < 65 || e.which > 90)) {
e.preventDefault();
}
});
上面的代码工作正常,只允许字母而不允许数字,但不允许空格.所以我想要的是它应该只允许字母(小写和大写字母),然后不应该允许数字和特殊字符以及除第一个字符外,还接受空格.请告诉我复制粘贴时如何限制.
The above code is working fine to allow only letters and not allow numbers but its not allow space.So what i want is it should allow only letters (both small and capital letters) then it should not allow numbers and special characters and also accept space except as a first character And please tell me how to restrict when copy paste.
推荐答案
可以使用HTML5属性pattern
吗?请参见 MDN文章有关更多信息.
Can you use the HTML5 attribute pattern
? See the MDN article on it for more information.
使用^[a-zA-Z][\sa-zA-Z]*
的正则表达式似乎可以满足您的要求.
Using a regex of ^[a-zA-Z][\sa-zA-Z]*
seems to cover your requirements.
所以像这样:
<div>Username:</div>
<input type="text" pattern="^[a-zA-Z][\sa-zA-Z]*" title="Can use upper and lower letters, and spaces but must not start with a space" />
这篇关于不允许使用空格作为第一个字符,并且只允许使用jquery的字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!