jquery密碼強度校驗

 
<script>
  
//下面的正則表達式建議各位收藏哦,項目上有可能會用得着
$( function (){ 
   $( '#pass' ).blur( function (e) {
     // alert('---------');
      //密碼爲八位及以上而且字母數字特殊字符三項都包括
      var strongRegex = new RegExp( "^(?=.{8,})(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*\\W).*$" , "g" );
    
    //密碼爲七位及以上而且字母、數字、特殊字符三項中有兩項,強度是中等
      var mediumRegex = new RegExp( "^(?=.{7,})(((?=.*[A-Z])(?=.*[a-z]))|((?=.*[A-Z])(?=.*[0-9]))|((?=.*[a-z])(?=.*[0-9]))).*$" , "g" );
      var enoughRegex = new RegExp( "(?=.{6,}).*" , "g" );
      if ( false == enoughRegex.test($( this ).val())) {
          $( '#passstrength' ).html( 'More Characters' );
      } else if (strongRegex.test($( this ).val())) {
          $( '#passstrength' ).className = 'ok' ;
          $( '#passstrength' ).html( 'Strong!' );
      } else if (mediumRegex.test($( this ).val())) {
          $( '#passstrength' ).className = 'alert' ;
          $( '#passstrength' ).html( 'Medium!' );
      } else {
          $( '#passstrength' ).className = 'error' ;
          $( '#passstrength' ).html( 'Weak!' );
      }
      return true ;
   });
});
</script>
          
<input type= "password" name= "pass" id= "pass" />
<span id= "passstrength" ></span>
相關文章
相關標籤/搜索