PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Fri Dec 13, 2019 10:36 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Thu Jun 02, 2005 5:29 am 
Offline
Forum Contributor
User avatar

Joined: Wed Jul 02, 2003 2:13 pm
Posts: 148
Location: West Country, UK
Hi,

Could somebody tell me how to write a regular expression that covers the following rules for a user name:

Must be 5 to 15 characters in length, and may only contain letters, underscores and hyphens.

I would need this for Javascript and for PHP, so if they use a different pattern, please could you show me both.

I have tried, truly I have. I've got bits working, but never the whole. Regular expressions and me were clearly never destined to cohabit the same point in time and space in any meaningful way! :wink:

Many thanks in advance.


Last edited by Jean-Yves on Thu Jun 02, 2005 9:06 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 02, 2005 6:09 am 
Offline
Breakbeat Nuttzer
User avatar

Joined: Wed Mar 24, 2004 8:57 am
Posts: 13098
Location: Melbourne, Australia
\w matches letters A-Z (upper and lowercase) and underscores so you just need that in a charcted class alogn with a hyphen with {5,15} for the length.
Use ^ and $ to constrain where the start and end parts are.
Syntax: [ Download ] [ Hide ]
$username1 = 'ipsy_dipsy'; //Woohoo

$username2 = 'i am a little st*r'; //Got spaces! Got * !



if (preg_match('/^ї\w-]{5,15}$/', $username1)) {

    echo $username1 .' is valid';

} else {

    echo $username1 .' is invalid';

}


and in javascript we use the test() method

Syntax: [ Download ] [ Hide ]
var username1 = 'ipsy_dipsy'; //Woohoo

var username2 = 'i am a little st*r'; //Got spaces! Got * !



var re = new RegExp('/^ї\\w-]{5,15}$/');



if (re.test(username1)) {

    alert(username1 +' is valid');

} else {

    alert(username1 +' is invalid');

}


Hope it helps :)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 02, 2005 9:05 am 
Offline
Forum Contributor
User avatar

Joined: Wed Jul 02, 2003 2:13 pm
Posts: 148
Location: West Country, UK


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group