确保用户名仅包含:AZ、az、.、_、-、0-9(无逗号)PHP

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12808002/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:15:58  来源:igfitidea点击:

Make sure username contains only: A-Z, a-z, ., _, -, 0-9 (no comma) PHP

phpregexstringvalidation

提问by user115422

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 仅限字母数字字符。

回答by Sam

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
}

回答by otporan

<?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_\-\.][email protected]$/', $mail)) {
                return true;    
            }
            return false;
        }
    }

    // The way to use this class

    $username = "otporan_123";
    $email = "[email protected]";

    $badUsername = "otporan*bad";
    $yahooEmail = "[email protected]";

    $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!

希望这可以帮助!

回答by circusdei

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

回答by chmeliuk

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