javascript - 带空格的姓氏、名字的正则表达式

标签 javascript regex

我正在使用下面的 JS 以这样的姓氏、名字格式输入姓名 Clark, Michael 现在我可能还需要允许这样的姓名 Tim Duncan, Vince Carter 这些名字之间有空格。

function validateName(txtbox) {
        var name = /^\w{1,20}\, \w{1,20}$/
        var check = document.getElementById(txtbox);
        if (check != null) {
            if (check.value != "") {
                if (!name.test(check.value)) {
                    alert('Please enter name in Last Name, First Name format');
                    document.getElementById(txtbox).focus();
                    return false;
                }
                else {
                    return true;
                }
            }
            else if (name.test(check.value)) {
                return true;
            }
            else if (check.value == "") {
                return true;
            }
        }
    }

有没有办法通过在同一个正则表达式中进行更改来实现这一点。非常感谢建议和帮助。

最佳答案

^[\w ]{1,20}\, [\w ]{1,20}$

这应该适合你。

关于javascript - 带空格的姓氏、名字的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27816630/

相关文章:

javascript - 如何从 Jade for Node.js 中的 js 脚本获取数据?

javascript - React 路由给出意外的 token 错误

javascript - pouchDB allDocs 返回未定义

javascript - 使用 JavaScript : prevent application inside DIV 在运行时编辑 CSS(更改选择器)

javascript - 相对于鼠标定位图像/背景图像

python - 通过正则表达式使用替代方法连接术语

javascript - Google 电子表格脚本

Sublime Text 片段中句点之前变量名称的正则表达式

regex - 如何使用正则表达式解析文本中没有尾部斜杠的路径?

regex - 如何使用 grep 命令列出基于点的名称