Validating Common Form Input - Part 2 Validating a username

简介:

用户名一般都是字母组成,包括[0-9],[a-z],可以是大小写。另外字符还有一些字符限制,比如$, #, £等。

一般用户名有一定的长度限制,必须去适应数据库对应栏位长度。

1.用户名必须是小写,且长度不超过16位

 
  1. function validate(form) {  
  2.   // Regular expression for username  
  3.   var rgx = /^[a-z]{0,16}$/;  
  4.   if(!rgx.test(form.elements.username.value))  
  5.     return false;  
  6.   return true;  
  7. }  

2. 前面基础上,用户名至少8个字符,至多16位。且可以是大小写字母

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

3.在前面基础上,允许字母和数字

 
  1. function validate(form) {  
  2.   // Regular expression for username  
  3.   var rgx = /^[a-z0-9]{8,16}$/i;  
  4.   if(!rgx.test(form.elements.username.value))  
  5.     return false;  
  6.   return true;  
  7. }  

4.忽略前面和后面的空格

 

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

 



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

相关文章
Invalid mapping pattern detected: /download/{{fileName}} ^Not allowed to nest variable c
Invalid mapping pattern detected: /download/{{fileName}} ^Not allowed to nest variable c
|
JavaScript
ESLint Parsing error: control-character-in-input-stream vue/no-parsing-error
ESLint Parsing error: control-character-in-input-stream vue/no-parsing-error
196 0
Gitlab报错:No authentication methods configured on login page
Gitlab报错:No authentication methods configured on login page
187 0
HttpMessageNotReadableException: Required request body is missing: xxx.controller.login(xxx)...
HttpMessageNotReadableException: Required request body is missing: xxx.controller.login(xxx)...
HttpMessageNotReadableException: Required request body is missing: xxx.controller.login(xxx)...