SoFunction
Updated on 2025-03-02

Regular matching password can only be a string combination of numbers and letters [php and js implementation]

This article describes the function of a regular matching password that can only be a combination of numbers and letters. Share it for your reference, as follows:

Password Requirements:

1. Can't be all numbers
2. Can't all be letters
3. Must be a combination of numbers and letters
4. Not containing special characters
5. String with password length 6-30 bits

/**
  * @desc get_pwd_strength()im: judge password structure based on password string
  * @param (string)$mobile
  * return Return: $msg
  */
function get_pwd_strength($pwd){
  if (strlen($pwd)>30 || strlen($pwd)<6)
  {
    return "The password must be a string of 6-30 bits";
  }
  if(preg_match("/^\d*$/",$pwd))
  {
    return "The password must contain letters, strength: weak";//All numbers  }
  if(preg_match("/^[a-z]*$/i",$pwd))
  {
    return "The password must contain numbers, strength: medium";//All letters  }
  if(!preg_match("/^[a-z\d]*$/i",$pwd))
  {
    return "Passwords can only contain numbers and letters, strength: strong";//There are numbers and letters ";  }
}

js regular match

/**
  * To detect password strength, it must be combined by numbers and letters, at least 6 digits of string.
  */
$.checkPwd = function(v){
 v=$.trim(v);
 if(<6||>30){
    return "Password length is 6-30 digits";
  }
  if(/^\d+$/.test(v))
  {
    return "All Numbers";
  }
  if(/^[a-z]+$/(v))
  {
    return "All letters";
  }
  if(!/^[A-Za-z0-9]+$/.test(v))
  {
    return "Only contain numbers and letters";
  }
  return "correct";
};

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

JavaScript regular expression online testing tool:
http://tools./regex/javascript

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

I hope this article will be helpful to everyone's regular expression learning.