-7

I am completely new to PHP. I need help writing a regex which validates a password. The password must be at least 8 chars in length, begin with a letter, end with a digit, and is case insensitive. The characters in between the first and last can be a digit, underscore, or symbol.

Any help would be greatly appreciated.

4

2 回答 2

2

/^[A-Za-z][0-9[:punct:]]{6,}[0-9]$/应该管用

这说:

  • 第一个字符必须是字母
  • 中间字符必须是数字或符号(包括下划线)
  • 必须至少有 6 个中间字符
  • 最后一个字符必须是数字
于 2013-07-11T18:46:42.887 回答
0

看看手册preg_match()中的PHP 函数。

快速示例:

<?php
// Check if the string is at least 8 chars long
if (strlen($password) < 8)
{
   // Password is too short
}


// Make the password "case insensitive"
$password = strtolower($password);


// Create the validation regex
$regex = '/^[a-z][\w!@#$%]+\d$/i';

// Validate the password
if (preg_match($regex, $password))
{
   // Password is valid
}
else
{
   // ... not valid
}

­

Regex Explanation:  
   ^           => begin of string
   [a-z]       => first character must be a letter
   [\w!@#$%]+  => chars in between can be digit, underscore, or symbol
   \d          => must end with a digit
   $           => end of string
   /i          => case insesitive
于 2013-07-11T19:03:57.743 回答