Hello can someone please help improve the regex I have:
/^[^\s].*[^\s]$/
It's intended to be used for a nickname field with the following criteria:
> no spaces
> exclude the following words(substring) within the nickname: 'anonymous', 'xxx', 'yyy'
> just allow letters, numbers, hyphens and underscores eg. exclude characters such as @, #, !, ~, %, ^, *, (, ), =, |, ,(comma) etc
Thanks
5 4372 Markus 6,050
Recognized Expert Expert
Try this on for size: -
<?php
-
-
$regex = '/[^a-zA-Z0-9_-]|anonymous|xxx|yyy/';
-
$nicks = array (
-
'anonymous', // Should fail
-
'xxy', // Should pass
-
'xxx', // Should fail
-
'yuy', // Should pass
-
'yyy', // Should fail
-
'mark', // Should pass
-
'mark!', // Should fail
-
'mark_skilbeck', // Should pass
-
'mark*skilbeck', // Should fail
-
'mark-skilbeck' // Should pass
-
);
-
-
foreach ($nicks as $nick) {
-
if (preg_match($regex, $nick)) {
-
printf('The nick "%s" did not pass validation.%s',
-
$nick, PHP_EOL);
-
} else {
-
printf('The nick "%s" passed validation.%s',
-
$nick, PHP_EOL);
-
}
-
}
-
You might also make the pattern case-insensitive by throwing the 'i' modifier onto the end of the pattern (/[^a-zA-Z0-9_-]|anonymous|xxx|yyy/ i).
Mark.
thanks so much for your post. the code you provided is much better than what i stated with but is it possible to exclude the words WITHIN the nickname.
i'm wanting to have a list of words that people can't use WITHIN a nickname. words like 'anonymous', 'admin', 'xxx', 'yyy'
eg. if your website is called 'BlueSky.com' you wouldn't want someone to register a nickname like 'BlueSkyAdmin' or 'BlueSkyStaff'.
Obviously i'd have to be careful as to which words would be excluded as they might make up legitimate nicknames. eg if 'now' was an excluded word that means that 'snowman' could not be registered.
exclude words: 'anonymous', 'admin', 'bluesky' 'xxx', 'yyy',
nicknames that should fail:
'admin'
'_admin'
'admin_user'
'BlueSky'
'BlueSkyAdmin'
'mrbluesky'
'mr-xxx'
'mrxxx'
'xxxyyy'
'mr_XxxYyy'
does anyone this this is overkill? i just don't want a malicious user registering a nickname pretending to be an authoritative type member.
Markus 6,050
Recognized Expert Expert
Um, if you add your restricted nicknames to the pattern (append them after a | pipe), then the pattern should match them. That is to say, 'test' will be matched in 'tester'.
Mark.
Dormilich 8,658
Recognized Expert Moderator Expert
note: - [^a-zA-Z0-9_-] = [^\w-]
-
[^a-zA-Z0-9_] = \W
Ahh cool, thanks guys the code you provided works perfectly
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Alan Pretre |
last post by:
Can anyone help me figure out a regex pattern for the following input
example:
xxx:a=b,c=d,yyy:e=f,zzz:www:g=h,i=j,l=m
I would want four matches from this:
1. xxx a=b,c=d
2. yyy e=f
3....
|
by: aevans1108 |
last post by:
expanding this message to microsoft.public.dotnet.xml
Greetings
Please direct me to the right group if this is an inappropriate place
to post this question. Thanks.
I want to format a...
|
by: Tim Conner |
last post by:
Hi,
Thanks to Peter, Chris and Steven who answered my previous answer about
regex to split a string. Actually, it was as easy as create a regex with the
pattern "/*-+()," and most of my string...
|
by: rjb |
last post by:
Hi there
I have a text file which looks like the one below.
I'm trying to write a program which will go through each of those lines
(for each section separated by an empty line) and give me:
...
|
by: rjb |
last post by:
Hi!
Could somebody have a look and help me to optimize the code below.
It may look like very bad way of coding, but this stuff is very, very new
for me.
I've included just few lines.
Regex...
| |
by: Rico |
last post by:
If there are consecutive occurrences of characters from the given
delimiter, String.Split() and Regex.Split() produce an empty string as the
token that's between such consecutive occurrences. It...
|
by: Bob |
last post by:
I think this is very simple but I am having difficult doing it. Basically
take a comma separated list:
abc, def, ghi, jk
A list with only one token does not have any commas:
abc
The first...
|
by: Jiggaz |
last post by:
Hi,
Look my stored procedure :
__________________
ALTER PROCEDURE dbo.CreateAccount
@Nickname varchar(30),
@Password varchar(15),
@Email varchar(50),
@Date datetime,
|
by: Flomo Togba Kwele |
last post by:
I am having difficulty writing a Regex constructor.
A line has a quote(") at its beginning and its end. I need to strip both characters off. If the
line looks like "1", I need the result to be 1....
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |