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

validate username

P: n/a
Hi,

Can anyone tell me how use regular expression to validate a username
with alpha numeric and special character but not space?

Thanks and Regards,

Jorn
Jul 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
<jo****@yahoo.com> wrote in message
news:e9**************************@posting.google.c om...
Hi,

Can anyone tell me how use regular expression to validate a username
with alpha numeric and special character but not space?

Thanks and Regards,

Jorn


Like this?

<?php
$test=array("test me", "testme", "test_me", "test89+");
foreach($test as $x) {
if(preg_match('/^[A-Za-z0-9_]*$/',$x))
print "$x was OK.\n";
else
print "$x wasn't OK.\n"
}
?>

HTH
Garp
Jul 17 '05 #2

P: n/a
On Sat, 03 Apr 2004 09:33:01 GMT, "Garp" <ga***@no7.blueyonder.co.uk> wrote:
if(preg_match('/^[A-Za-z0-9_]*$/',$x))


if(preg_match('/^\w+$/',$x))

Does the same thing except shorter, and doesn't accept empty usernames.

--
Andy Hassall <an**@andyh.co.uk> / Space: disk usage analysis tool
http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space
Jul 17 '05 #3

P: n/a
> if(preg_match('/^\w+$/',$x))

how if i want to match username with space?

thanks & regards

Jorn
Jul 17 '05 #4

P: n/a
On Mon, 05 Apr 2004 04:27:59 -0700, jo****@yahoo.com wrote:
if(preg_match('/^\w+$/',$x))


how if i want to match username with space?

thanks & regards

Jorn

if (preg_match('/^(\w|\s)+$/', $x))
'\s' defines a space. The (...|...) will select either (\w or \s) so you
cover both.
HTH.

Regards,

Ian

--
Ian.H
digiServ Network
London, UK
http://digiserv.net/

Jul 17 '05 #5

P: n/a
On 5 Apr 2004 04:27:59 -0700, jo****@yahoo.com (jo****@yahoo.com) wrote:
if(preg_match('/^\w+$/',$x))


how if i want to match username with space?


/^[\w ]+$/

--
Andy Hassall <an**@andyh.co.uk> / Space: disk usage analysis tool
http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space
Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.