确保用户名只包含:A-Z, A-Z, ., _, -, 0-9(没有逗号)PHP[复制]

时间:2022-09-11 18:30:28

Possible Duplicate:
regular expression for letters, numbers and - _

可能的副本:字母、数字和- _的正则表达式。

I am creating a signup form in PHP and need to ensure that user's usernames don't contain unwanted characters. Is there anyway I can create a function that returns true for A-Z a-z 0-9 - . _.

我在PHP中创建了一个注册表单,需要确保用户的用户名不包含不需要的字符。无论如何,我可以创建一个函数,它返回a-z a-z - 0-9。_。

Also I do not want the user's emails to be from yahoo as for some reason they reject the confirmation emails sent. Along with __FILTER_VALIDATE_EMAIL__what would I need to add?

我也不希望用户的电子邮件来自雅虎,因为某些原因他们拒绝了发送的确认邮件。与__filter_validate_email__一起,我需要添加什么?

PS: is there anything wrong with the characters I have mentioned above? I have noted that gmail doesn't allow -_ only . and YouTube only alpha-numeric characters.

PS:我上面提到的人物有什么问题吗?我注意到gmail不允许-_。而YouTube只有字母数字字符。

4 个解决方案

#1


8  

Edited to use \w instead of a-zA-Z0-9_

编辑使用\w而不是a-zA-Z0-9_。

if(preg_match("/[^\w-.]/", $user)){
    // invalid character
}
if(!filter_var($email, FILTER_VALIDATE_EMAIL) || strstr($email,'@yahoo.com')) {
    // either invalid email, or it contains in @yahoo.com
}

#2


2  

if(preg_match("/[^-A-Za-z0-9._ ]/", $userName)){
    // there are one or more of the forbidden characters (the set of which is unknown)
}

#3


2  

<?php

    // The validator class

    class Validator
    {
        public function isValidUsername($username)
        {
            if(preg_match('/^[a-zA-Z0-9_\-\.]+$/', $username)) {
                return true;    
            }
            return false;
        }

        public function isYahooMail($mail) {
            if(preg_match('/^[a-zA-Z0-9_\-\.]+@yahoo.com$/', $mail)) {
                return true;    
            }
            return false;
        }
    }

    // The way to use this class

    $username = "otporan_123";
    $email = "otporan@gmail.com";

    $badUsername = "otporan*bad";
    $yahooEmail = "otporan@yahoo.com";

    $validator = new Validator();

    var_export($validator->isValidUsername($username));
    echo "<br />";

    var_export($validator->isValidUsername($badUsername));
    echo "<br />";

    var_export($validator->isYahooMail($email));
    echo "<br />";

    var_export($validator->isYahooMail($yahooEmail));
    echo "<br />";  

?>

This code would return: true false false true

这段代码将返回:true false false true。

This is a class, but you can see whats going on in methods and write your own functions if you like procedural code :)

这是一个类,但您可以看到方法中发生了什么,如果您喜欢过程代码,可以编写自己的函数:)

Hope this helps!

希望这可以帮助!

#4


-1  

if (!preg_match('/\w\-/', $username) {
    //throw error 
}

#1


8  

Edited to use \w instead of a-zA-Z0-9_

编辑使用\w而不是a-zA-Z0-9_。

if(preg_match("/[^\w-.]/", $user)){
    // invalid character
}
if(!filter_var($email, FILTER_VALIDATE_EMAIL) || strstr($email,'@yahoo.com')) {
    // either invalid email, or it contains in @yahoo.com
}

#2


2  

if(preg_match("/[^-A-Za-z0-9._ ]/", $userName)){
    // there are one or more of the forbidden characters (the set of which is unknown)
}

#3


2  

<?php

    // The validator class

    class Validator
    {
        public function isValidUsername($username)
        {
            if(preg_match('/^[a-zA-Z0-9_\-\.]+$/', $username)) {
                return true;    
            }
            return false;
        }

        public function isYahooMail($mail) {
            if(preg_match('/^[a-zA-Z0-9_\-\.]+@yahoo.com$/', $mail)) {
                return true;    
            }
            return false;
        }
    }

    // The way to use this class

    $username = "otporan_123";
    $email = "otporan@gmail.com";

    $badUsername = "otporan*bad";
    $yahooEmail = "otporan@yahoo.com";

    $validator = new Validator();

    var_export($validator->isValidUsername($username));
    echo "<br />";

    var_export($validator->isValidUsername($badUsername));
    echo "<br />";

    var_export($validator->isYahooMail($email));
    echo "<br />";

    var_export($validator->isYahooMail($yahooEmail));
    echo "<br />";  

?>

This code would return: true false false true

这段代码将返回:true false false true。

This is a class, but you can see whats going on in methods and write your own functions if you like procedural code :)

这是一个类,但您可以看到方法中发生了什么,如果您喜欢过程代码,可以编写自己的函数:)

Hope this helps!

希望这可以帮助!

#4


-1  

if (!preg_match('/\w\-/', $username) {
    //throw error 
}