我想阻止输入中带重音字母(任何语言)的条目,最好是我希望通过正则表达式通过属性pattern来完成此阻止

我尝试了一些但是没有成功...



<form>
  <label for="username">Name <i>(only letters without accent)</i></label>
  <br>
  <input name="username" id="username" type="text" pattern="[A-Za-z]+" oninvalid="this.setCustomValidity('Only letters without accent')">
</form>





接受:Joao SilvaPedroFabio Duarte ...

拒绝:João SilvaPedro CamõesFábio Duarte ...

最佳答案

<input name="username" id="username" type="text"
    pattern="[A-Za-z ]*" title="Latin letters and space characters only"> />


测试此代码here



或者,您可以控制键入期间允许使用哪些字符。

<input name="username" id="username" type="text" onCopy="return false"
    onDrag="return false" onDrop="return false" onPaste="return false"
    autocomplete=off />


jQuery的:

$(document).ready(function() {
    $("#username").keypress(function(event) {
        var inputValue = event.which;
        if(!((inputValue >= 65 && inputValue <=  90) ||  // A-Z
             (inputValue >= 97 && inputValue <= 122) ||  // a-z
             (inputValue == 32))) {                      // space
            event.preventDefault();
        }
    });
});


测试此代码here

关于javascript - 正则表达式可防止输入中带有重音字母,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50470235/

10-11 11:56