Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    New Coder
    Join Date
    Aug 2005
    Posts
    70
    Thanks
    0
    Thanked 0 Times in 0 Posts

    validate username w/ preg_match

    I'm new to the preg_match and haven't seen a good tutorial that really explains it to people that are completely new, example, me. I want to validate a username, it can contain lowercase, uppercase and and underscore. But it can't start or end with an underscore. So 700lbGorilla, 700lb_Gorilla, 700_lb_Gorilla are ok, but _700lbGorilla and 700lbGorilla_ are not.

    PHP Code:
    preg_match('/^[a-zA-Z0-9][\w][a-zA-Z0-9]$/'$name
    But that didn't work to well, i'm pretty sure the first bracket is right, but after that I'm lost.

  • #2
    Senior Coder NancyJ's Avatar
    Join Date
    Feb 2005
    Location
    Bradford, UK
    Posts
    3,174
    Thanks
    19
    Thanked 66 Times in 65 Posts
    PHP Code:
    preg_match('/^[a-zA-Z0-9][\w]+[a-zA-Z0-9]$/'$name
    you need to specify that they can have more than 1 \w

  • #3
    New Coder
    Join Date
    Aug 2005
    Posts
    70
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for the reply, I changed the + to a *. Than way if there isn't a _ it still is valid.

  • #4
    Senior Coder NancyJ's Avatar
    Join Date
    Feb 2005
    Location
    Bradford, UK
    Posts
    3,174
    Thanks
    19
    Thanked 66 Times in 65 Posts
    \w is any character or digit... not just underscores

  • #5
    New Coder
    Join Date
    Aug 2005
    Posts
    70
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I know I meant I used a * incase there is no \w. UNless I'm still lost and ther both do the same.

  • #6
    Senior Coder NancyJ's Avatar
    Join Date
    Feb 2005
    Location
    Bradford, UK
    Posts
    3,174
    Thanks
    19
    Thanked 66 Times in 65 Posts
    ^[a-zA-Z0-9][\w]+[a-zA-Z0-9]$ means
    the first character must be a letter or a number, the next one or more characters can be any word character and the last character must be a number or letter.
    Which means the string must be at least 3 characters long.

    ^[a-zA-Z0-9][\w]*[a-zA-Z0-9]$
    means the first character must be a letter or number, the next zero or more characters can be any word character and the last character must be a number or letter.
    Which means the string must be at least 2 characters long (the first and last)

  • #7
    New Coder
    Join Date
    Aug 2005
    Posts
    70
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok, thank you, that clears it up.

  • #8
    New Coder
    Join Date
    Aug 2005
    Posts
    70
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I really suck at this topic, wouldn't

    PHP Code:
    if(eregi('/^[a-z]+$/' 
    be only lowercase letters?

  • #9
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Yes. You could add an "i" at the end if you don't care about the case.
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #10
    New Coder
    Join Date
    Aug 2005
    Posts
    70
    Thanks
    0
    Thanked 0 Times in 0 Posts
    PHP Code:
    if(!empty($fname)) {
            if(
    eregi('/^[a-z]+$/'$fname)) {
                
    $f ucfirst(strtolower($fname));
            } else {
                
    $f FALSE;
                echo 
    '<p><font color="red">Your name contains invalid characters.</font></p>';
            }
        } else {
            
    $f $fname;
        } 
    Typing in "asdsd" comes out false.

  • #11
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Try trim($fname) and then the regex.
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •