By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,501 Members | 2,805 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,501 IT Pros & Developers. It's quick & easy.

Stumped on REGEX and PHP

P: 41
Hi all. I've finally submitted to asking for online help. I been trying to do it on my own, but it seems like I've hit a really hard rock.

I have a function that looks like this

function validusername($username, $pattern = "/^(?!.*[-_.]{2})[a-zA-Z\d][\w.-]{4,36}[a-zA-Z\d]$/") {
if ( (strlen($username) < 6) || (strlen($username) > 40) || (!preg_match($username, $pattern)) ) {
return false;
else {
return true;


The part I'm having trouble with is the regular expression. I'm using the same regex in javascript, but I can't get this function to work out in PHP.

This is the error I get:
Warning: preg_match() [function.preg-match]: Delimiter must not be alphanumeric or backslash

It would be appreciated if anyone could help me on this one.

Dec 7 '07 #1
Share this Question
Share on Google+
1 Reply

P: 41
I'm sorry I didn't include any details. Here it is.

1. This function is used to validate a username.
2. The regular expression basically says the following
a) upper case characters are allowed
b) lower case characters are allowed
c) numbers are allowed
d) string should be 6-40 characters
e) hypens(-) and . (dot) are allowed but cannot end the string and cannot be consecutive

AHHHHHHH.... as I was typing I found out my mistake. I feel like an idiot...

ok for preg_match I had

preg_match($username, $pattern) but it should be the opposite preg_match($pattern, $username).

anyway, just in case anyone has the same problem just look at this post so you don't humiliate yourself ^_^.
Dec 7 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.