0

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 - . _.

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?

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.

Community
  • 1
  • 1
user115422
  • 4,662
  • 10
  • 26
  • 38
  • 1
    please allow me to chose what ever user-name i want,not arbitrarily restrict me. Thanks. –  Oct 09 '12 at 20:41
  • @Dragon What do you want to know my site? :) – user115422 Oct 09 '12 at 20:43
  • @MuqMan I want all sites to stop imposing meaningless restrictions. –  Oct 09 '12 at 20:47
  • @mario, its not a duplicate because i want a . as well ;) and I __never__ found that post! – user115422 Oct 09 '12 at 20:51
  • 1
    @MuqMan: That happens to be included. If you couldn't be bothered to make such a trivial addition, then you're wrong on Stackoverflow. And there many more duplicates. Objectively too little search effort. – mario Oct 09 '12 at 20:54
  • @mario im sure you can tell that i am not too frequent to SO, also i _did_ google it, plus I don't know the rules of prey_match! Sorry if this seems against your terms! – user115422 Oct 09 '12 at 20:58

4 Answers4

8

Edited to use \w instead of 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
}
Sam
  • 20,096
  • 2
  • 45
  • 71
2
if(preg_match("/[^-A-Za-z0-9._ ]/", $userName)){
    // there are one or more of the forbidden characters (the set of which is unknown)
}
circusdei
  • 1,967
  • 12
  • 28
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

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!

otporan
  • 1,345
  • 2
  • 12
  • 29
  • 1
    great! I'll try it out! Looks like you're introducing me to classes and OOP ;) – user115422 Oct 09 '12 at 20:53
  • I wanted to mark both as answers but right now i am not doing OOP much :) srry not to give you the full points but I still plus1d your answer (not google plus ;)) – user115422 Oct 09 '12 at 23:47
-1
if (!preg_match('/\w\-/', $username) {
    //throw error 
}
chmeliuk
  • 1,060
  • 9
  • 8