Single names 和username 不同,一般位字母。数字长度位(4,32)
- function validate(form) {
- // Regular expression for a single name
- var rgx = /^\s*[a-z]{4,32}\s*$/i;
- if(!rgx.test(form.elements.name.value))
- return false;
- return true;
- }
2.为了支持本地化。可以超找unicode.他依赖使用的encoding,普遍的为:ISO-8859-1 and UTF-8.
- <meta http-equiv=\"Content-Type\" content=\"text/html;charset=ISO-8859-1\" />
如果是ISO-8859-1,对于特殊的丹麦字符如 æ, ø and å 。他们在ASCII对应的值为 230, 248 and 314。十六进制:0xE6, 0xF8 and 0xE5
在名字中允许丹麦字符如 æ, ø and å
- function validate(form) {
- // Regular expression for a single name
- var rgx = /^\s*[a-z\xe6\xf8\xe5]{4,32}\s*$/i;
- if(!rgx.test(form.elements.name.value))
- return false;
- return true;
- }
如果是UTF-8,可以直接输入字符
- // Regular expression for a single name
- var rgx = /^\s*[a-zæøå]{4,32}\s*$/i;
- if(!rgx.test(form.elements.name.value))
- return false;
- return true;
话说回来,一般会有多个名称,允许有空白字符
- function validate(form) {
- // Regular expression for a multiple names
- var rgx = /^\s*[a-z\s]{4,32}\s*$/i;
- if(!rgx.test(form.elements.name.value))
- return false;
- return true;
- }
显然现在,也不是我们想要的。因为添加的\s,可以匹配4到32个空白字符
- function validate(form) {
- // Regular expression for a multiple names
- var rgx = /^\s*[a-z]{4,}(\s+[a-z]{4,})*\s*$/i;
- if(!rgx.test(form.elements.name.value))
- return false;
- return true;
- }
添加长度限制
- function validate(form) {
- // Shortcut to save writing
- var elm = form.elements;
- // Regular expression for a multiple names
- var rgx = /^\s*[a-z]{4,}(\s+[a-z]{4,})*\s*$/i;
- if(elm.name.value.length < 33 && !rgx.test(elm.name.value))
- return false;
- return true;
- }
处理名称中缩写、及破折号问题。like Martin
, Martin L. King
, M. Luther-King
, Martin Luther-King
etc.
- function validate(form) {
- // Shortcut to save writing
- var elm = form.elements;
- // Regular expression for a multiple names
- var rgx = /^\s*[a-z]+([a-z]*\.|\s*\-\s*[a-z]+)?(\s+[a-z]+(\.|\s*\-\s*[a-z]+)?)*\s*$/i;
- if(elm.name.value.length < 33 && !rgx.test(elm.name.value))
- return false;
- return true;
- }
本文转自 randy_shandong 51CTO博客,原文链接:http://blog.51cto.com/dba10g/456636,如需转载请自行联系原作者