3

I am checking username entered by user

I'm trying to validate usernames in PHP using preg_match() but I can't seem to get it working the way I want it. I require preg_match() to:

  • accept only letters , numbers and . - _

i.e. alphanumeric dot dash and underscore only, i tried regex from htaccess which is like this

([A-Za-z0-9.-_]+)

like this way but it doesnt seem to work, it giving false for simple alpha username.

$text = 'username';

if (preg_match('/^[A-Za-z0-9.-_]$/' , $text)) {
   echo 'true';   
} else {
   echo 'false';
}

How can i make it work ?

i am going to use it in function like this

//check if username is valid
function isValidUsername($str) {
    return preg_match('/[^A-Za-z0-9.-_]/', $str);
}

i tried answwer in preg_match() and username but still something is wrong in the regex.


update

I am using code given by xdazz inside function like this.

//check if username is valid
function isValidUsername($str) {
    if (preg_match('/^[A-Za-z0-9._-]+$/' , $str)) {
       return true;   
    } else {
       return false;
    }
}

and checking it like

$text = 'username._-546_546AAA';


if (isValidUsername($text) === true) {
echo 'good';
}
else{
echo 'bad';
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
  • 1
    if (preg_match('/^[A-Za-z0-9.-_]$/' , $text)) here a + or * is required.or else it will match a single character – vks Aug 08 '14 at 05:42

4 Answers4

2

You missed the +(+ for one or more, * for zero or more), or your regex only matches a string with one char.

if (preg_match('/^[A-Za-z0-9._-]+$/' , $text)) {
   echo 'true';   
} else {
   echo 'false';
}
xdazz
  • 158,678
  • 38
  • 247
  • 274
  • thanks, this is working as expected, i tried escaping - like this \- but it didnt worked,. –  Aug 08 '14 at 05:42
2

hyphen - has special meaning inside [...] that is used for range.

It should be in the beginning or in the last or escape it like ([A-Za-z0-9._-]+) otherwise it will match all the character that is in between . and _ in ASCII character set.

Read similar post Including a hyphen in a regex character bracket?

Better use \w that matches [A-Za-z0-9_]. In shorter form use [\w.-]+


What is the meaning for your last regex pattern?

Here [^..] is used for negation character set. If you uses it outside the ^[...] then it represents the start of the line/string.

[^A-Za-z0-9.-_]          any character except: 
                         'A' to 'Z', 
                         'a' to 'z', 
                         '0' to '9',
                         '.' to '_'
Community
  • 1
  • 1
Braj
  • 46,415
  • 5
  • 60
  • 76
  • [\w.-]+ this looks nice, trying it out. –  Aug 08 '14 at 05:48
  • 1
    I don't think so that there will be any significant performance improvement. Read [Regex Performance Optimization Tips and Tricks](http://stackoverflow.com/questions/1252194/regex-performance-optimization-tips-and-tricks) – Braj Aug 08 '14 at 05:56
  • Thanks for pointing out. `.-_` inside `[]` will match char from 46-95 in char table. I was having the same error in my regex. Put `-` at the end fixed the issue. I believe escape it with `\-` will also work. – LeOn - Han Li Mar 21 '17 at 15:35
1

Just put - at the last in character class and add + after the char class to match one or more characters.

$text = 'username';

if (preg_match('/^[A-Za-z0-9._-]+$/' , $text)) {
   echo 'true';   
} else {
   echo 'false';
}
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
1

function should be like this

function isValidUsername($str) {
    return preg_match("/^[A-Za-z0-9._-]+$/", $str);
}
Satish Sharma
  • 9,547
  • 6
  • 29
  • 51