Validating Common Form Input - Part 3 Validating names

简介:

Single names  和username 不同,一般位字母。数字长度位(4,32)

 
  1. function validate(form) {  
  2.   // Regular expression for a single name  
  3.   var rgx = /^\s*[a-z]{4,32}\s*$/i;  
  4.   if(!rgx.test(form.elements.name.value))  
  5.     return false;  
  6.   return true;  
  7. }  

2.为了支持本地化。可以超找unicode.他依赖使用的encoding,普遍的为:ISO-8859-1 and UTF-8.

 
  1. <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 å

 
  1. function validate(form) {  
  2.   // Regular expression for a single name  
  3.   var rgx = /^\s*[a-z\xe6\xf8\xe5]{4,32}\s*$/i;  
  4.   if(!rgx.test(form.elements.name.value))  
  5.     return false;  
  6.   return true;  
  7. }  

如果是UTF-8,可以直接输入字符

 
  1. // Regular expression for a single name  
  2. var rgx = /^\s*[a-zæøå]{4,32}\s*$/i;  
  3. if(!rgx.test(form.elements.name.value))  
  4.   return false;  
  5. return true;  

话说回来,一般会有多个名称,允许有空白字符

 
  1. function validate(form) {  
  2.   // Regular expression for a multiple names  
  3.   var rgx = /^\s*[a-z\s]{4,32}\s*$/i;  
  4.   if(!rgx.test(form.elements.name.value))  
  5.     return false;  
  6.   return true;  
  7. }  

显然现在,也不是我们想要的。因为添加的\s,可以匹配4到32个空白字符

 
  1. function validate(form) {  
  2.   // Regular expression for a multiple names  
  3.   var rgx = /^\s*[a-z]{4,}(\s+[a-z]{4,})*\s*$/i;  
  4.   if(!rgx.test(form.elements.name.value))  
  5.     return false;  
  6.   return true;  
  7. }  

添加长度限制

 

 
  1. function validate(form) {  
  2.   // Shortcut to save writing  
  3.   var elm = form.elements;  
  4.   // Regular expression for a multiple names  
  5.   var rgx = /^\s*[a-z]{4,}(\s+[a-z]{4,})*\s*$/i;  
  6.   if(elm.name.value.length < 33 && !rgx.test(elm.name.value))  
  7.     return false;  
  8.   return true;  
  9. }  

 

处理名称中缩写、及破折号问题。like MartinMartin L. KingM. Luther-KingMartin Luther-King etc.

 
  1. function validate(form) {  
  2.   // Shortcut to save writing  
  3.   var elm = form.elements;  
  4.   // Regular expression for a multiple names  
  5.   var rgx = /^\s*[a-z]+([a-z]*\.|\s*\-\s*[a-z]+)?(\s+[a-z]+(\.|\s*\-\s*[a-z]+)?)*\s*$/i;  
  6.   if(elm.name.value.length < 33 && !rgx.test(elm.name.value))  
  7.     return false;  
  8.   return true;  
  9. }  

 



本文转自 randy_shandong 51CTO博客,原文链接:http://blog.51cto.com/dba10g/456636,如需转载请自行联系原作者

相关文章
Invalid mapping pattern detected: /download/{{fileName}} ^Not allowed to nest variable c
Invalid mapping pattern detected: /download/{{fileName}} ^Not allowed to nest variable c
GenImageInterleaved Wrong type of control parameter 1
GenImageInterleaved Wrong type of control parameter 1
使用pageHelper报错 Type definition error: [simple type, classXXXX]
使用pageHelper报错 Type definition error: [simple type, classXXXX]
config.guess: unable to guess system type、config.sub: missing argument
config.guess: unable to guess system type、config.sub: missing argument
123 0
|
Java
The type XXX cannot be resolved. It is indirectly referenced from required .class files
The type XXX cannot be resolved. It is indirectly referenced from required .class files
112 0
|
算法
On the Correct and Complete Enumeration of the Core Search Space
在之前的文章中我们讨论了基于graph的DP-based算法,来解决join ordering的枚举问题。 这些DP算法通过join predicate描述的连通性,解决了枚举可能的表组合问题,但join graph本身(即使hypergraph)是无法完整的描述join语义的,因为连通边本身无法描述不同类型的join语义,例如left outer join/semi join/anti join...,因此即使找到了所谓的csg-cmp-pair,也不一定是有效的plan。 这篇paper讨论的就是这个问题,当枚举出一个csg-cmp-pair (S1 o S2),如何判断这是有效的join
430 0
On the Correct and Complete Enumeration of the Core Search Space
SAP IDoc Post不成功,报错 - A company code cannot be determined for LI 0000100061 –
SAP IDoc Post不成功,报错 - A company code cannot be determined for LI 0000100061 –
SAP IDoc Post不成功,报错 - A company code cannot be determined for LI 0000100061 –
2015-03-18 - Deliberately pass a wrong note type for my task creation
2015-03-18 - Deliberately pass a wrong note type for my task creation
88 0
2015-03-18 - Deliberately pass a wrong note type for my task creation
|
Java
Hybris DDIC type and its counterpart model class
Hybris DDIC type and its counterpart model class
124 0
Hybris DDIC type and its counterpart model class