Home > Web Front-end > JS Tutorial > Regular matching password can only be a combination of numbers and letters string function_regular expression

Regular matching password can only be a combination of numbers and letters string function_regular expression

微波
Release: 2017-06-28 13:46:51
Original
3688 people have browsed it

This article mainly introduces the function of regular matching passwords that can only be combinations of numbers and letters String, involving techniques related to regular operations such as characters and numbers, and provides examples of PHP and JS implementation. Friends who need it can refer to

The example in this article describes the function of regular matching passwords that can only be a combination of numbers and letters. Share it with everyone for your reference, the details are as follows:

Password requirements:

1. It cannot be all numbers
2. It cannot be all letters
3. Must be a combination of numbers and letters
4. Does not contain special characters
5. The password must be a string of 6-30 characters

/**
 * @desc get_pwd_strength()im:根据密码字符串判断密码结构
 * @param (string)$mobile
 * return 返回:$msg
 */
function get_pwd_strength($pwd){
  if (strlen($pwd)>30 || strlen($pwd)<6)
  {
    return "密码必须为6-30位的字符串";
  }
  if(preg_match("/^\d*$/",$pwd))
  {
    return "密码必须包含字母,强度:弱";//全数字
  }
  if(preg_match("/^[a-z]*$/i",$pwd))
  {
    return "密码必须包含数字,强度:中";//全字母
  }
  if(!preg_match("/^[a-z\d]*$/i",$pwd))
  {
    return "密码只能包含数字和字母,强度:强";//有数字有字母 ";
  }
}
Copy after login

js Regular matching

/**
 * 检测密码强度,必须由数字与字母组合,至少6位的字符串。
 */
$.checkPwd = function(v){
 v=$.trim(v);
 if(v.length<6||v.length>30){
    return "密码长度为6-30位";
  }
  if(/^\d+$/.test(v))
  {
    return "全数字";
  }
  if(/^[a-z]+$/i.test(v))
  {
    return "全字母";
  }
  if(!/^[A-Za-z0-9]+$/.test(v))
  {
    return "只能含有数字有字母";
  }
  return "正确";
};
Copy after login

PS: Here are 2 very convenient regular expression tools for your reference:

JavaScript Regular expression online testing tool:
http://tools.jb51.net/regex/javascript

Regular expression online generation tool:
http://tools.jb51.net/regex/create_reg

The above is the detailed content of Regular matching password can only be a combination of numbers and letters string function_regular expression. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template