Javascript javascript验证中名称字段的正则表达式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14088714/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 15:42:10  来源:igfitidea点击:

Regular expression for name field in javascript validation

javascript

提问by user1179752

i need a regular expression in javascript validation. Regular expression for name field that will accept alphabets and only space character between words and total characters in the field should be in between 2 and 30. i.e., the field should accept min 2 chars and max of 30 chars

我需要在 javascript 验证中使用正则表达式。名称字段的正则表达式将接受字母和单词之间的空格字符和字段中的总字符应在 2 到 30 之间。即,该字段应接受最少 2 个字符和最多 30 个字符

回答by Adeel Ahmed

function validate(id)
{
    var regex = /^[a-zA-Z ]{2,30}$/;
    var ctrl =  document.getElemetnById(id);

    if (regex.test(ctrl.value)) {
        return true;
    }
    else {
        return false;
    }
}

回答by skub

Try this:

尝试这个:

/^([a-zA-Z ]){2,30}$/

回答by sourcecode

your solution would be.

你的解决方案是。

 /^[a-zA-Z ]{2,30}$/;

Note: just add a space within the brackets. other whitespace characters like -tab, form feed, and line feed ,will not match.

注意:只需在括号内添加一个空格。其他空白字符,如 -tab、换页符和换行符,将不匹配。