You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

姓名输入框验证优化需求:需支持全名中间空格输入

Fixing Name Validation to Allow Spaces in Full Names

Hey, let's get this sorted out. Your current validation only lets users type letters and backspace, but we need to add support for spaces (since full names often have them). Here's the adjusted code with the fix:

<html>
<body>
<form method="POST" action="">
    <br>
    <label>Name</label>
    <input type="text" name="name" required onkeypress="return checkNum(event)" />
    <br><br>
    <input type="submit" value="submit" />
</form>
</body>
<script>
function checkNum(event) {
    // Allow uppercase letters, lowercase letters, backspace, and space bar
    if ((event.keyCode >= 65 && event.keyCode <= 90) || 
        (event.keyCode >= 97 && event.keyCode <= 122) || 
        event.keyCode === 8 || 
        event.keyCode === 32) {
        return true;
    } else {
        alert("Please enter only letters and spaces for your name");
        return false;
    }
}
</script>
</html>

Key Adjustments Made:

  • Added support for the space bar by including event.keyCode === 32 in our allowed conditions (32 is the keyCode for the space bar)
  • Fixed the original condition's imprecision: your initial check event.keyCode > 64 would have accidentally allowed some symbols (like [ which is keyCode 91), so we switched to exact ranges for uppercase (65-90) and lowercase (97-122) letters
  • Updated the alert message to clearly inform users that spaces are now permitted

This tweak will let users input full names like "John Michael Doe" while still blocking invalid characters such as numbers, symbols, or special characters.

内容的提问来源于stack exchange,提问作者Amal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 06:34:25