JS函数只允许输入字母和空格

JS function to allow enter only letters and white spaces

本文关键字:空格 输入 函数 许输入 JS      更新时间:2023-09-26

>我需要一个jquery或js函数只允许输入字母和空格。提前谢谢。

页:

<p:inputText onkeypress="onlyLetter(this)">

功能:

function onlyLetter(input){
    $(input).keypress(function(ev) {
   var keyCode = window.event ? ev.keyCode : ev.which;
  //  code
    });
}

以下代码仅允许 a-z、A-Z 和空格。

.HTML

<input id="inputTextBox" type="text" />

jQuery

$(document).on('keypress', '#inputTextBox', function (event) {
    var regex = new RegExp("^[a-zA-Z ]+$");
    var key = String.fromCharCode(!event.charCode ? event.which : event.charCode);
    if (!regex.test(key)) {
        event.preventDefault();
        return false;
    }
});

注意:键盘事件,自 2020 年 1 月 1 日起已弃用

只需使用要禁用或阻止工作的键/数字的 ascii 代码(十进制值)。ASCII 表 .

.HTML:

<input id="inputTextBox" type="text" />

j查询 :

$(document).ready(function(){
    $("#inputTextBox").keydown(function(event){
        var inputValue = event.which;
        // allow letters and whitespaces only.
        if(!(inputValue >= 65 && inputValue <= 120) && (inputValue != 32 && inputValue != 0)) { 
            event.preventDefault(); 
        }
    });
});

jsFiddle 演示

首先,我在jQuery方面几乎没有经验,我将提供一个普通的javascript示例。在这里:

document.getElementById('inputid').onkeypress=function(e){
    if(!(/[a-z ]/i.test(String.fromCharCode(e.keyCode))) {
        e.preventDefault();
        return false;
    }
}

调整Ashad Shanto回答了一下。请注意,如果使用脚本,则无法键入 y 和 z。您必须将输入值从 120 更改为 123。以下是 ASCII 表参考: http://ee.hawaii.edu/~tep/EE160/Book/chap4/subsection2.1.1.1.html 使用以下脚本键入所有字母、空格和退格键。

<script>
    $(document).ready(function(){
        $("#inputTextBox").keypress(function(event){
            var inputValue = event.which;
            // allow letters and whitespaces only.
            if(!(inputValue >= 65 && inputValue <= 123) && (inputValue != 32 && inputValue != 0)) { 
                event.preventDefault(); 
            }
            console.log(inputValue);
        });
    });
</script>

您可以使用我从这篇文章中获取的这个简单方法

<input type="text" name="fullName" onkeypress="return (event.charCode > 64 && 
event.charCode < 91) || (event.charCode > 96 && event.charCode < 123)" 
placeholder="Full Name">

jQuery

var letters = /^[A-Za-z ]+$/;
var city_input="";
$(document).ready(function(){
    $("#city_name").on("input", function(){
        var city_value=$(this).val();
        if(city_value==="")
        {
            city_input=city_value;
        }
        else if(city_value.match(letters)===null){
            $(this).val(city_input);
        }
        else{
            city_input=city_value;
        }
    });
});

这是您可以轻松理解的代码,并且可以针对任何字符char异常进行修改。

我包括BACKSPACE的例外.

同样,您可以通过在语句中包含键代码来给出例外。

var c= ((e.which>=65 && e.which<91) || (e.which==8 /**Here 8 if for the Backspace**/) || (e.which=="your key code"))

https://gist.github.com/SathishSaminathan/e3c509243ead20fcae26c87fdd6f78fd

<!DOCTYPE html>
<html>
<head>
<script
  src="https://code.jquery.com/jquery-3.4.1.js"></script>
</head>
<body>
<br/>
Enter Only Alphabate: <input id="txtName" name="lname">
 
 <script>
 $('#txtName').keypress(function (e) {
        var regex = new RegExp("^[a-zA-Z 's]+$");
        var str = String.fromCharCode(!e.charCode ? e.which : e.charCode);
        if (regex.test(str)) {
            return true;
        }
        else
        {
        e.preventDefault();
        alert('Please Enter Alphabate');
        return false;
        }
    });
 </script>
</body>
</html>

对我来说

,在 z 之后在输入标签中添加一个空格是有效的。

<p:inputText onkeydown="return /[a-z ]/i.test(event.key)">
function ValidateAlpha(evt) { 
  var keyCode = (evt.which) ? evt.which : evt.keyCode if (
    (keyCode < 65 || keyCode > 90) && 
    (keyCode < 97 || keyCode > 123) && 
    keyCode != 32 && 
    keyCode != 39
  )