正则表达式实例(JS验证)

   // 密码是英数字且不能为空
     function  checkPass1(val)
    {
        
var  pattChar = new  RegExp( ' [0-9a-zA-Z]{1,} ' );
        
if (val == '' )
        {
            document.getElementById(
' lblInfo ' ).innerHTML = ' 密码不能为空 ' ;
        }
        
else   if (pattChar.exec(val) == val)
        {
            document.getElementById(
' lblInfo ' ).innerHTML = ' 密码正确 ' ;
        }
        
else
        {
            document.getElementById(
' lblInfo ' ).innerHTML = ' 密码只能是英数字 ' ;
        }
    }
    
    
// 根据输入文本判断密码强度,密码只能是英数字,且长度必须大于6
     // 强度标准:
     // 弱-全是数字
     // 强-当长度大于8,且包括英文字符
     // 中-其它
     function  checkPass2(val)
    {
        
var  i;
        
var  c;
        
var  iCharCount = 0 ;
        
var  iNumCount = 0 ;
        
var  iOtherCount = 0 ;
        
var  pattChar = new  RegExp( ' [a-zA-Z] ' );
        
var  pattNum = new  RegExp( ' [0-9] ' );
        
        
for (i = 0 ;i < val.length;i ++ )
        {
            c
= val.substr(i, 1 );
            
if (pattChar.exec(c) != null )
            {
                
// 找到字符
                iCharCount ++ ;
            }
            
else   if (pattNum.exec(c) != null )
            {
                
// 找到数字
                iNumCount ++ ;
            }
            
else
            {
                
// 其它字符
                iOtherCount ++ ;
            }
        }
        
        
if (iOtherCount > 0 )
        {
            document.getElementById(
' lblInfo ' ).innerHTML = ' 密码只能是英数字 ' ;
        }
        
else
        {
            
if ((iCharCount + iNumCount) < 6 )
            {
                document.getElementById(
' lblInfo ' ).innerHTML = ' 密码长度至少六位 ' ;
            }
            
else   if (iCharCount == 0 )
            {
                document.getElementById(
' lblInfo ' ).innerHTML = ' 密码强度-弱 ' ;
            }
            
else   if (iCharCount > 0   &&  (iCharCount + iNumCount) > 8 )
            {
                document.getElementById(
' lblInfo ' ).innerHTML = ' 密码强度-强 ' ;
            }
            
else
            {
                document.getElementById(
' lblInfo ' ).innerHTML = ' 密码强度-中 ' ;
            }
        }
        
    }
    
// 移除字符串首尾空格
     function  deleteBlank(control)
    {
        
var  text = control.value;
        text
= text.replace( / ^\s{1,} / g, '' ); 
        text
= text.replace( / \s{1,}$ / g, '' );  
        control.value
= text; 
    }


// 调用方法
< asp:TextBox ID = " txtPassWord "  onblur = " checkPass2(this.value); "  runat = " server " >< / asp:TextBox>

你可能感兴趣的:(正则表达式)