0

My client ask me to add new a rule to the username to his website login. Therefore I've tried the following code.

  $userid = "12345678SM"; //format of the username
  $check="/^[0-9]+[S].[M]{10}$/i";
  $check2="/^[0-9]+[F].[M]{10}$/i";
    print($name);
  if(!preg_match($check,$userid) || !preg_match($check2,$userid)) {
    print('The user id is invalid');                    
  }

However, even though I entered the correct format of the username it's printing the error. I've actually used this code sometimes ago and worked but here I don't understand why it's not working. Could anyone please help me out of this ?

FYI: Format of the username should be 7 digits and 2 selected alphabet characters eg: SM or FM

4

2 回答 2

3

[M]{10}表示需要 10 M。

正确的检查是:

$check="/^\d{7}[SF]M$/i";
if (!preg_match($check, $userid)) {
    print('The user id is invalid');                    
}
于 2013-03-22T20:53:27.027 回答
1

[M]{10}”部分匹配“MMMMMMMMMM”。您描述的格式可以检查

^[0-9]{8}[S|F]M$

你还说

$userid = "12345678SM"; (8位数

用户名格式应为7 位数字和 2 个选定的字母字符

我的示例适用于 8。

于 2013-03-22T20:55:32.630 回答